Given integer variables, say N and K. The task is to first compute the permutations of N and then rearrange the permutations so that they are a distance K from each element.
Let's look at various input and output scenarios -input- int n = 20, int k = 2
output
strong>− Rearrange the first N numbers so that they are at K distance: 3 4 1 2 7 8 5 6 11 12 9 10 15 16 13 14 19 20 17 18.Explanation
Explanation strong>− We are given integer variables 'N' i.e. 20 and 'K' i.e. 2. Now we will calculate the permutations 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 The elements will be arranged in such a way that all elements are "k" away from each element.
Input− int n = 10, int k = 3
Input− int n = 10, int k = 3
Input p> Output - Rearrange the first N numbers so that they are at K distance: impossible Explanation - We give The integer variable 'N' is 10, and 'K' is 3. Now we will calculate the permutations of 'N' i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Now, we will arrange the elements in such a way that all elements are "k" distance away from each element, but that is not possible for the given input value. Input an integer type element, namely 'N' and 'K'. Call the function Rearrangement(int n, int k) by passing N and K as parameters to the function. Internal function Rearrangement(int n, int k) Declare an integer variable as temp and set it to n % (2 * k ). Declare an integer array as ptr 1 of size n, that is, prt[n 1]. Check IF k = 0 and then start looping FOR from i to 1 until i is less than size and increase i by 1 and print i. Check IF temp is not equal to 0, then print NOT POSSIBLE. Start the FOR loop from i to 1 until i is less than Start the FOR loop from i to 1 until i is less than n, and set i to i 2 * k. Inside the loop, start another loop FOR from j to 1 until j is less than k and increment j by 1. Inside the loop, the swa method is called by passing ptr[i j -1] and ptr[k i j - 1] as arguments. Start a FOR loop from i to 1 until i is less than N and increment i by 1. Print prt[i]. Print the result. If we run the above code it will generate the following outputThe method used in the following program is as follows
#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
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
The above is the detailed content of Rearrange first N numbers so that they are K distance apart in C++. For more information, please follow other related articles on the PHP Chinese website!