Rearrange first N numbers to make them at K distance
Last Updated :
28 Jan, 2022
Given a positive number K, we need to permute first N natural numbers in such a way that the absolute distance of each permuted number from its original position is K and if it is not possible to rearrange them in such a 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 the 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 the 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++
#include <bits/stdc++.h>
using namespace std;
void printRearrangeNnumberForKDistance( int N, int K)
{
if (K == 0)
{
for ( int i = 1; i <= N; i++)
cout << i << " " ;
cout << endl;
return ;
}
if (N % (2 * K) != 0)
{
cout << "Not Possible\n" ;
return ;
}
int arr[N + 1];
for ( int i = 1; i <= N; i++)
arr[i] = i;
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]);
for ( int i = 1; i <= N; i++)
cout << arr[i] << " " ;
}
int main()
{
int N = 12, K = 3;
printRearrangeNnumberForKDistance(N, K);
return 0;
}
|
Java
class GFG
{
static void printRearrangeNnumberForKDistance( int N, int K)
{
if (K == 0 )
{
for ( int i = 1 ; i <= N; i++)
System.out.print(i + " " );
System.out.println();
return ;
}
if (N % ( 2 * K) != 0 )
{
System.out.print( "Not Possible\n" );
return ;
}
int arr[]= new int [N + 1 ];
for ( int i = 1 ; i <= N; i++)
arr[i] = i;
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;
}
for ( int i = 1 ; i <= N; i++)
System.out.print(arr[i] + " " );
}
public static void main (String[] args)
{
int N = 12 , K = 3 ;
printRearrangeNnumberForKDistance(N, K);
}
}
|
Python3
def printRearrangeNnumberForKDistance(N, K):
if (K = = 0 ):
for i in range ( 1 , N + 1 ):
print (i, end = " " );
print ();
return ;
if (N % ( 2 * K) ! = 0 ):
print ( "Not Possible" );
return ;
arr = [ 0 ] * (N + 1 );
for i in range ( 1 , N + 1 ):
arr[i] = i;
for i in range ( 1 , N + 1 ,
2 * K):
for j in range ( 1 , K + 1 ):
temp = arr[i + j - 1 ];
arr[i + j - 1 ] = arr[K + i + j - 1 ];
arr[K + i + j - 1 ] = temp;
for i in range ( 1 , N + 1 ):
print (arr[i], end = " " );
if __name__ = = '__main__' :
N = 12 ;
K = 3 ;
printRearrangeNnumberForKDistance(N, K);
|
C#
using System;
class GFG
{
static void printRearrangeNnumberForKDistance( int N, int K)
{
if (K == 0)
{
for ( int i = 1; i <= N; i++)
Console.Write(i + " " );
Console.WriteLine();
return ;
}
if (N % (2 * K) != 0)
{
Console.Write( "Not Possible\n" );
return ;
}
int []arr= new int [N + 1];
for ( int i = 1; i <= N; i++)
arr[i] = i;
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;
}
for ( int i = 1; i <= N; i++)
Console.Write(arr[i] + " " );
}
public static void Main ()
{
int N = 12, K = 3;
printRearrangeNnumberForKDistance(N, K);
}
}
|
Javascript
<script>
function printRearrangeNnumberForKDistance(N, K)
{
if (K == 0)
{
for (let i = 1; i <= N; i++)
document.write(i + " " );
document.write( "<br/>" );
return ;
}
if (N % (2 * K) != 0)
{
document.write( "Not Possible\n" );
return ;
}
let arr = [];
for (let i = 1; i <= N; i++)
arr[i] = i;
for (let i = 1; i <= N; i += 2 * K)
for (let j = 1; j <= K; j++)
{
let temp = arr[i + j - 1];
arr[i + j - 1] = arr[K + i + j - 1];
arr[K + i + j - 1] = temp;
}
for (let i = 1; i <= N; i++)
document.write(arr[i] + " " );
}
let N = 12, K = 3;
printRearrangeNnumberForKDistance(N, K);
</script>
|
Output :
4 5 6 1 2 3 10 11 12 7 8 9
Share your thoughts in the comments
Please Login to comment...