LeetCode Problem Workspace

Permutations IV

Find the k-th alternating permutation of numbers 1 to n, ensuring no adjacent numbers share parity, using array and math strategies.

category

4

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · Array plus Math

bolt

Answer-first summary

Find the k-th alternating permutation of numbers 1 to n, ensuring no adjacent numbers share parity, using array and math strategies.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus Math

Try AiBox Copilotarrow_forward

This problem asks for the k-th lexicographical alternating permutation of the first n positive integers, where no two adjacent numbers are both even or both odd. Start by analyzing the array positions using combinatorics to count valid sequences. Apply math to directly determine the first number and recursively select the next elements, efficiently skipping invalid branches.

Problem Statement

Given two integers n and k, generate permutations of numbers 1 through n such that no two consecutive numbers are both odd or both even. These permutations are called alternating permutations. Your task is to return the k-th alternating permutation in lexicographical order.

If there are fewer than k alternating permutations for the given n, return an empty array. For example, with n = 4 and k = 6, the lexicographically sorted alternating permutations produce [3,4,1,2] as the sixth permutation.

Examples

Example 1

Input: n = 4, k = 6

Output: [3,4,1,2]

The lexicographically-sorted alternating permutations of [1, 2, 3, 4] are: Since k = 6 , we return [3, 4, 1, 2] .

Example 2

Input: n = 3, k = 2

Output: [3,2,1]

The lexicographically-sorted alternating permutations of [1, 2, 3] are: Since k = 2 , we return [3, 2, 1] .

Example 3

Input: n = 2, k = 3

Output: []

The lexicographically-sorted alternating permutations of [1, 2] are: There are only 2 alternating permutations, but k = 3 , which is out of range. Thus, we return an empty list [] .

Constraints

  • 1 <= n <= 100
  • 1 <= k <= 1015

Solution Approach

Precompute counts with combinatorics

Calculate the number of valid alternating permutations for each starting number using combinatorial formulas. This helps skip large invalid segments and reduces recursion depth.

Recursive selection by parity

Select numbers recursively, alternating between odd and even positions, and decrement k based on skipped counts. Use math to directly determine which number occupies each position.

Construct result array efficiently

Once the correct choices are determined, fill the result array in order without generating all permutations. This prevents memory overhead and ensures time efficiency for large n.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity varies from O(n^2) to O(n!) depending on whether combinatorial counting or naive generation is used. Space complexity depends on recursion depth and storing partial arrays, typically O(n).

What Interviewers Usually Probe

  • They may ask how to count valid alternating permutations without generating them all.
  • Expect a follow-up about handling large n and k efficiently with math rather than brute-force.
  • They might probe edge cases where n is odd and the first element must be odd.

Common Pitfalls or Variants

Common pitfalls

  • Failing to alternate parity correctly, producing adjacent numbers with the same parity.
  • Attempting to generate all permutations, leading to timeouts for n > 10.
  • Miscounting skipped permutations when decrementing k, causing off-by-one errors.

Follow-up variants

  • Return all alternating permutations instead of just the k-th.
  • Allow repeated numbers and generate k-th alternating sequence with repetitions.
  • Find the k-th permutation with a different adjacency constraint, e.g., no two consecutive numbers differ by 1.

FAQ

What defines an alternating permutation in Permutations IV?

An alternating permutation has no two consecutive numbers sharing the same parity; adjacent numbers must alternate between odd and even.

How do I find the k-th permutation without generating all permutations?

Use combinatorial counting to skip invalid branches and recursively select numbers based on remaining k and parity constraints.

What happens if k exceeds the number of valid permutations?

Return an empty array since there are fewer than k alternating permutations possible.

Is there a shortcut for large n values?

Precompute counts and use math-based selection to avoid full permutation generation and reduce recursion.

Can this method handle n up to 100 and k up to 10^15?

Yes, by combining combinatorial counting with efficient recursive selection, avoiding brute-force generation.

terminal

Solution

Solution 1

#### Python3

1
Permutations IV Solution: Array plus Math | LeetCode #3470 Hard