Given two integers, n and k, an alternating permutation is a permutation of the first n positive integers such that no two adjacent elements are both odd or both even.
Return the k-th alternating permutation sorted in lexicographical order. If there are fewer than k valid alternating permutations, return an empty list.
Example 1:
Input: n = 4, k = 6
Output: [3,4,1,2]
Explanation:
The lexicographically-sorted alternating permutations of [1, 2, 3, 4] are:
[1, 2, 3, 4][1, 4, 3, 2][2, 1, 4, 3][2, 3, 4, 1][3, 2, 1, 4][3, 4, 1, 2] ← 6th permutation[4, 1, 2, 3][4, 3, 2, 1]Since k = 6, we return [3, 4, 1, 2].
Example 2:
Input: n = 3, k = 2
Output: [3,2,1]
Explanation:
The lexicographically-sorted alternating permutations of [1, 2, 3] are:
[1, 2, 3][3, 2, 1] ← 2nd permutationSince k = 2, we return [3, 2, 1].
Example 3:
Input: n = 2, k = 3
Output: []
Explanation:
The lexicographically-sorted alternating permutations of [1, 2] are:
[1, 2][2, 1]There are only 2 alternating permutations, but k = 3, which is out of range. Thus, we return an empty list [].
Constraints:
1 <= n <= 1001 <= k <= 1015Problem Overview: You are given n and an index k. The task is to construct the k-th permutation of numbers 1..n that satisfies the required parity ordering constraint (odd and even placement rules). Instead of generating all permutations, you need to enumerate only the valid ones and return the k-th in lexicographic order.
Approach 1: Brute Force Permutation Enumeration (O(n! * n) time, O(n) space)
Generate every permutation of 1..n using backtracking or next_permutation. For each permutation, check whether the parity condition holds (for example, alternating odd/even positions). Maintain a counter of valid permutations and stop once the k-th valid one appears. This approach is easy to implement but extremely slow because the permutation count grows factorially. Even with pruning, the worst‑case time is O(n! * n) since each permutation must be validated.
Approach 2: Combinatorics + Lexicographic Enumeration (O(n^2) time, O(n) space)
Instead of generating permutations explicitly, count how many valid permutations start with a given prefix. Split the numbers into odd and even groups, then determine which type of number must appear at each index according to the parity rule. When constructing the permutation, iterate through the remaining candidates in sorted order. For each candidate, temporarily place it and compute how many valid permutations can be formed with the remaining numbers using combinatorial counting (factorials of remaining odd/even counts).
If the number of permutations starting with that candidate is less than k, skip the entire block and subtract that count from k. Otherwise, fix that number in the permutation and continue building the next position. This technique avoids generating unnecessary permutations and directly jumps to the correct lexicographic block. Counting relies on simple factorial math and tracking how many odd and even values remain.
This strategy combines combinatorics with controlled enumeration. The array of remaining numbers is updated after each placement, so candidate scanning adds an O(n) factor per position, giving an overall O(n^2) time complexity with O(n) auxiliary space.
Recommended for interviews: The combinatorics counting approach is what interviewers expect. Brute force shows that you understand permutation generation, but it fails scalability. The optimized solution demonstrates stronger reasoning about permutation blocks, factorial counting, and constrained enumeration across an array of remaining values.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutation Generation | O(n! * n) | O(n) | Useful only for very small n or for validating correctness during development. |
| Combinatorics + Lexicographic Enumeration | O(n^2) | O(n) | Best general solution. Efficiently jumps over permutation blocks using factorial counting. |
3470. Permutations IV (Leetcode Hard) • Programming Live with Larry • 325 views views
Watch 2 more video solutions →Practice Permutations IV with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor