602. Permutations III

Given an integer n, generate and return all possible alternating permutations of numbers from 1 to n, arranged in lexicographical order.

A permutation of the first n natural numbers is called an alternating permutation if no two consecutive numbers in the sequence are either both odd or both even.

Example 1:

Input: n = 4

Output: [[1,2,3,4], [1,4,3,2], [2,1,4,3], [2,3,4,1], [3,2,1,4], [3,4,1,2], [4,1,2,3], [4,3,2,1]]

Example 2:

Input: n = 2

Output: [[1,2], [2,1]]

Now Your Turn!

Pick the correct output for the given input

Input: n = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n ≤ 10

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<vector<int>> permute(int n) {
}
 
};
 
Test Case

Input:

N