# Rearrange first N numbers to make them at K distance

Given a positive number K, we need to permute first N natural numbers in such a way that absolute distance of each permuted number from its original position is K and if it is not possible to rearrange them in such manner then print not possible.

**Examples :**

Input : N = 12 K = 2 Output : [3 4 1 2 7 8 5 6 11 12 9 10] Explanation : Initial permutation is [1 2 3 4 5 6 7 8 9 10 11 12] In rearrangement, [3 4 1 2 7 8 5 6 11 12 9 10] we have all elements at distance 2. Input : N = 12 K = 3 Output : [4 5 6 1 2 3 10 11 12 7 8 9]

We can solve this problem by finding the pattern in solutions. If we go through many solutions manually, we can see that if we partition N numbers into slots of size 2K, then each slot can be rearranged in two parts of size K, where the difference of position with actual position will be K.

Example 1 :N = 12 and K = 2 First 12 numbers are partitioned into 2*2 = 4 sized 12/4 = 3 slots as shown below, [[1 2 3 4] [5 6 7 8] [9 10 11 12]] Now each half of the slot is swapped so that, every number will go at K position distance from its initial position as shown below, [[3 4 1 2] [7 8 5 6] [11 12 9 10]]Example 2 :N = 12 and K = 3, [1 2 3 4 5 6 7 8 9 10 11 12] [[1 2 3 4 5 6] [7 8 9 10 11 12]] [[4 5 6 1 2 3] [10 11 12 7 8 9]] [4 5 6 1 2 3 10 11 12 7 8 9] Which is the final rearrangement for N = 12 and K = 3

In below code, the case when K = 0 is handled separately by printing all numbers in their actual order. When N is not divisible by 2K, ‘not possible’ is printed directly.

## C++

`// C++ program to rearrange permuatations to make ` `// them K distance away ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Method prints permutation of first N numbers, ` `where each number is K distance away from its ` `actual position */` `void` `printRearrangeNnumberForKDistance(` `int` `N, ` `int` `K) ` `{ ` ` ` `// If K = 0, then print numbers in their natural ` ` ` `// order ` ` ` `if` `(K == 0) ` ` ` `{ ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `cout << i << ` `" "` `; ` ` ` `cout << endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// If N doesn't divide (2K) evenly, then ` ` ` `// rearrangement is not possible ` ` ` `if` `(N % (2 * K) != 0) ` ` ` `{ ` ` ` `cout << ` `"Not Possible\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Copy first N numbers to an auxiliary ` ` ` `// array ` ` ` `int` `arr[N + 1]; ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `arr[i] = i; ` ` ` ` ` `// Swap halves of each 2K sized slot ` ` ` `for` `(` `int` `i = 1; i <= N; i += 2 * K) ` ` ` `for` `(` `int` `j = 1; j <= K; j++) ` ` ` `swap(arr[i + j - 1], arr[K + i + j - 1]); ` ` ` ` ` `// Print the rearranged array ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 12, K = 3; ` ` ` `printRearrangeNnumberForKDistance(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to rearrange permuatations ` `// to make them K distance away ` ` ` `class` `GFG ` `{ ` ` ` `/* Method prints permutation of first N numbers, ` ` ` `where each number is K distance away from its ` ` ` `actual position */` ` ` `static` `void` `printRearrangeNnumberForKDistance(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `// If K = 0, then print numbers ` ` ` `// in their natural order ` ` ` `if` `(K == ` `0` `) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `System.out.println(); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// If N doesn't divide (2K) evenly, then ` ` ` `// rearrangement is not possible ` ` ` `if` `(N % (` `2` `* K) != ` `0` `) ` ` ` `{ ` ` ` `System.out.print(` `"Not Possible\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Copy first N numbers to an auxiliary ` ` ` `// array ` ` ` `int` `arr[]=` `new` `int` `[N + ` `1` `]; ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `arr[i] = i; ` ` ` ` ` `// Swap halves of each 2K sized slot ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i += ` `2` `* K) ` ` ` `for` `(` `int` `j = ` `1` `; j <= K; j++) ` ` ` `{ ` ` ` `int` `temp = arr[i + j - ` `1` `]; ` ` ` `arr[i + j - ` `1` `] = arr[K + i + j - ` `1` `]; ` ` ` `arr[K + i + j - ` `1` `] = temp; ` ` ` `} ` ` ` ` ` `// Print the rearranged array ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `System.out.print(arr[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `N = ` `12` `, K = ` `3` `; ` ` ` `printRearrangeNnumberForKDistance(N, K); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to rearrange permuatations ` `// to make them K distance away ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `/* Method prints permutation of first N numbers, ` ` ` `where each number is K distance away from its ` ` ` `actual position */` ` ` `static` `void` `printRearrangeNnumberForKDistance(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `// If K = 0, then print numbers ` ` ` `// in their natural order ` ` ` `if` `(K == 0) ` ` ` `{ ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `Console.WriteLine(); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// If N doesn't divide (2K) evenly, then ` ` ` `// rearrangement is not possible ` ` ` `if` `(N % (2 * K) != 0) ` ` ` `{ ` ` ` `Console.Write(` `"Not Possible\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Copy first N numbers to an auxiliary ` ` ` `// array ` ` ` `int` `[]arr=` `new` `int` `[N + 1]; ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `arr[i] = i; ` ` ` ` ` `// Swap halves of each 2K sized slot ` ` ` `for` `(` `int` `i = 1; i <= N; i += 2 * K) ` ` ` `for` `(` `int` `j = 1; j <= K; j++) ` ` ` `{ ` ` ` `int` `temp = arr[i + j - 1]; ` ` ` `arr[i + j - 1] = arr[K + i + j - 1]; ` ` ` `arr[K + i + j - 1] = temp; ` ` ` `} ` ` ` ` ` `// Print the rearranged array ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `Console.Write(arr[i] + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `N = 12, K = 3; ` ` ` `printRearrangeNnumberForKDistance(N, K); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

**Output :**

4 5 6 1 2 3 10 11 12 7 8 9

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

## Recommended Posts:

- Number of ways to make mobile lock pattern
- Sum of all numbers that can be formed with permutations of n digits
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Make all combinations of size k
- Number of ways to get even sum by choosing three numbers from 1 to N
- Different ways to sum n using numbers greater than or equal to m
- Palindromic Selfie Numbers
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Probability for three randomly chosen numbers to be in AP
- Minimum numbers needed to express every integer below N as a sum
- Count Numbers with N digits which consists of odd number of 0's
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Count numbers whose difference with N is equal to XOR with N
- Ways of selecting men and women from a group to make a team
- N digit numbers divisible by 5 formed from the M digits