Given two integers n and k. Consider first permutation of natural n numbers, P = “1 2 3 … n”, print a permutation “Result” such that abs(Resulti – Pi) = k where Pi denotes the position of i in permutation P. The value of Pi varies from 1 to n. If there are multiple possible results, then print the lexicographically smallest one.
Input: n = 6 k = 3 Output: 4 5 6 1 2 3 Explanation: P = 1 2 3 4 5 6 Result = 4 5 6 1 2 3 We can notice that the difference between individual numbers (at same positions) of P and result is 3 and "4 5 6 1 2 3" is lexicographically smallest such permutation. Other greater permutations could be Input : n = 6 k = 2 Output : Not possible Explanation: No permutation is possible with difference is k
Naive approach is to generate all the permutation from 1 to n and pick the smallest one which satisfy the condition of absolute difference k. Time complexity of this approach is Ω(n!) which will definitely time out for large value of n.
The Efficient approach is to observe the pattern at each position of index. For each position of index i, there can only exist two candidate i.e., i + k and i – k. As we need to find lexicographically smallest permutation so we will first look for i – k candidate(if possible) and then for i + k candidate.
Illustration: n = 8, k = 2 P : 1 2 3 4 5 6 7 8 For any ith position we will check which candidate is possible i.e., i + k or i - k 1st pos = 1 + 2 = 3 (1 - 2 not possible) 2nd pos = 2 + 2 = 4 (2 - 2 not possible) 3rd pos = 3 - 2 = 1 (possible) 4th pos = 4 - 2 = 2 (possible) 5th pos = 5 + 2 = 7 (5 - 2 already placed, not possible) 6th pos = 6 + 2 = 8 (6 - 2 already placed, not possible) 7th pos = 7 - 2 = 5 (possible) 8th pos = 8 - 2 = 6 (possible)
Note: If we observe above illustration then we will find that i + k and i – k are alternating after kth consecutive interval. Another observation is that the whole permutation is only when n is even such that n can be divided into two parts where each part must be divisible by k.
4 5 6 1 2 3 Not Possible 3 4 1 2 7 8 5 6
Time complexity: O(n)
Auxiliary space: O(1)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum number of given operations required to convert a permutation into an identity permutation
- Permutation Coefficient
- Inverse Permutation
- Find the possible permutation of the bits of N
- Find the permutation p from the array q such that q[i] = p[i+1] - p[i]
- Number of permutation with K inversions
- Generate all permutation of a set in Python
- Permutation and Combination in Python
- BogoSort or Permutation Sort
- Largest permutation after at most k swaps
- Find the minimum permutation of A greater than B
- Find n-th lexicographically permutation of a string | Set 2
- Increasing permutation of first N natural numbers
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Combination and Permutation Practice Questions | Set 1