
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange first N numbers to make them at K distance in C++
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
- Related Articles
- Rearrange String k Distance Apart in C++
- Rearrange string so that same character become n distance apart JavaScript
- C++ Program to get number at position k after positioning n natural numbers
- Print all increasing sequences of length k from first n natural numbers in C++
- Python – Remove Elements in K distance with N
- Count numbers < = N whose difference with the count of primes upto them is > = K in C++
- Python program to replace first ‘K’ elements by ‘N’
- Program to find first N Iccanobif Numbers in C++
- Program to generate first n lexicographic numbers in python
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Maximum XOR using K numbers from 1 to n in C++
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Lambda expression in Python to rearrange positive and negative numbers
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Program to find sum of first n natural numbers in C++
