# Rearrange first N numbers to make them at K distance in C++

C++Server Side ProgrammingProgramming

We are given integer variables, let's say, N and K. The task is to firstly calculate the permutation of N and then rearrange the permutation in such a manner that it will be K distance from every element.

## Let us see various input output scenarios for this −

Input − int n = 20, int k = 2

Output − Rearrangement of first N numbers to make them at K distance is: 3 4 1 2 7 8 5 6 11 12 9 10 15 16 13 14 19 20 17 18.

Explanation − we are given integer variables ‘N’ i.e. 20 and ‘K’ i.e. 2. Now we will calculate the permutation of ‘N’ i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18. 19, 20. Now, we will arrange the elements in such a manner that all the elements will be at ‘k’ distance from every element.

Input − int n = 10, int k = 3

Output − Rearrangement of first N numbers to make them at K distance is: Not Possible

Explanation − we are given integer variables ‘N’ i.e. 10 and ‘K’ i.e. 3. Now we will calculate the permutation of ‘N’ i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Now, we will arrange the elements in such a manner that all the elements will be at ‘k’ distance from every element but its not possible with the given input values.

## Approach used in the below program is as follows

• Input an integer type elements i.e. ‘N’ and ‘K’.

• Call to the function Rearrangement(int n, int k) by passing N and K to a function as a parameter.

• Inside the function Rearrangement(int n, int k)

• Declare an integer variable as temp and set it with n % (2 * k).

• Declare an integer type array as ptr of size n + 1 i.e. prt[n+1].

• Check IF k = 0 then start loop FOR from i to 1 till i less than size and increment the i by 1 and print i.

• Check IF temp not equals 0 then print NOT POSSIBLE.

• Start loop FOR from i to 1 till i less than N and set ptr[i] with i.

• Start loop FOR from i to 1 till i less than n and set i with i + 2 * k. Inside the loop, start another loop FOR from j to 1 till j less than k and increment the j by 1. Inside the loop, call a swa method by passing ptr[i + j -1] and ptr[k + i + j - 1] as a parameter.

• Start loop FOR from i to 1 tll i is less than N and increment the i by 1.

• Print prt[i].

• Print the result.

## Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int n, int k){
int temp = n % (2 * k);
int ptr[n + 1];
if(k == 0){
for(int i = 1; i <= n; i++){
cout << i << " ";
}
return;
}
if(temp != 0){
cout<<"Not Possible";
return;
}
for(int i = 1; i <= n; i++){
ptr[i] = i;
}
for(int i = 1; i <= n; i += 2 * k){
for(int j = 1; j <= k; j++){
swap(ptr[i + j - 1], ptr[k + i + j - 1]);
}
}
for(int i = 1; i <= n; i++){
cout << ptr[i] << " ";
}
}
int main(){
int n = 20;
int k = 2;
cout<<"Rearrangement of first N numbers to make them at K distance is: ";
Rearrangement(n, k);
return 0;
}

## Output

If we run the above code it will generate the following Output

Rearrangement of first N numbers to make them at K distance is: 3 4 1 2 7 8 5 6 11 12 9 10 15 16 13 14 19 20 17 18
Updated on 02-Nov-2021 06:57:56