An alternating permutation is a fascinating mathematical concept where adjacent elements follow a strict pattern of alternating between odd and even numbers. Given two integers n and k, you need to find the k-th alternating permutation of the first n positive integers when all valid alternating permutations are sorted in lexicographical order.
In an alternating permutation, no two adjacent elements can both be odd or both be even. For example, [1, 2, 3, 4] is alternating because 1(odd) → 2(even) → 3(odd) → 4(even), but [1, 3, 2, 4] is not because 1(odd) → 3(odd).
Goal: Return the k-th alternating permutation in lexicographical order, or an empty list if fewer than k valid permutations exist.
Input & Output
Time & Space Complexity
For each position (n), we may need to scan remaining numbers (O(n))
Store result array and lists of remaining odd/even numbers
Constraints
- 1 ≤ n ≤ 10
- 1 ≤ k ≤ 106
- Numbers are the first n positive integers: [1, 2, 3, ..., n]
- Adjacent elements in alternating permutation cannot have same parity