Given two integers n and k. Consider first permutation of natural n numbers, P = “1 2 3 … n”, print a permutation “Result” such that abs(Resulti – Pi) = k where Pi denotes the position of i in permutation P. The value of Pi varies from 1 to n. If there are multiple possible results, then print the lexicographically smallest one.
Input: n = 6 k = 3
Output: 4 5 6 1 2 3
Explanation:
P = 1 2 3 4 5 6
Result = 4 5 6 1 2 3
We can notice that the difference between
individual numbers (at same positions) of
P and result is 3 and "4 5 6 1 2 3" is
lexicographically smallest such permutation.
Other greater permutations could be
Input : n = 6 k = 2
Output : Not possible
Explanation: No permutation is possible
with difference is k
Naive approach is to generate all the permutation from 1 to n and pick the smallest one which satisfy the condition of absolute difference k. Time complexity of this approach is ?(n!) which will definitely time out for large value of n.
The Efficient approach is to observe the pattern at each position of index. For each position of index i, there can only exist two candidate i.e., i + k and i – k. As we need to find lexicographically smallest permutation so we will first look for i – k candidate(if possible) and then for i + k candidate.
Illustration:
n = 8, k = 2
P : 1 2 3 4 5 6 7 8
For any ith position we will check which candidate
is possible i.e., i + k or i - k
1st pos = 1 + 2 = 3 (1 - 2 not possible)
2nd pos = 2 + 2 = 4 (2 - 2 not possible)
3rd pos = 3 - 2 = 1 (possible)
4th pos = 4 - 2 = 2 (possible)
5th pos = 5 + 2 = 7 (5 - 2 already placed, not possible)
6th pos = 6 + 2 = 8 (6 - 2 already placed, not possible)
7th pos = 7 - 2 = 5 (possible)
8th pos = 8 - 2 = 6 (possible)
Note: If we observe above illustration then we will find that i + k and i – k are alternating after kth consecutive interval. Another observation is that the whole permutation is only when n is even such that n can be divided into two parts where each part must be divisible by k.
C++
#include<bits/stdc++.h>
using namespace std;
void kDifferencePermutation( int n, int k)
{
if (!k)
{
for ( int i = 0; i < n; ++i)
cout << i + 1 << " " ;
}
else if (n % (2 * k) != 0)
cout << "Not Possible" ;
else
{
for ( int i = 0; i < n; ++i)
{
if ((i / k) % 2 == 0)
cout << i + k + 1 << " " ;
else
cout << i - k + 1 << " " ;
}
}
cout << "\n" ;
}
int main()
{
int n = 6 , k = 3;
kDifferencePermutation(n, k);
n = 6 , k = 2;
kDifferencePermutation(n, k);
n = 8 , k = 2;
kDifferencePermutation(n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void kDifferencePermutation( int n,
int k)
{
if (!(k > 0 ))
{
for ( int i = 0 ; i < n; ++i)
System.out.print( i + 1 + " " );
}
else if (n % ( 2 * k) != 0 )
System.out.print( "Not Possible" );
else
{
for ( int i = 0 ; i < n; ++i)
{
if ((i / k) % 2 == 0 )
System.out.print( i + k
+ 1 + " " );
else
System.out.print( i - k
+ 1 + " " );
}
}
System.out.println() ;
}
static public void main (String[] args)
{
int n = 6 , k = 3 ;
kDifferencePermutation(n, k);
n = 6 ;
k = 2 ;
kDifferencePermutation(n, k);
n = 8 ;
k = 2 ;
kDifferencePermutation(n, k);
}
}
|
Python3
def kDifferencePermutation(n, k):
if (k = = 0 ):
for i in range (n):
print (i + 1 , end = " " )
elif (n % ( 2 * k) ! = 0 ):
print ( "Not Possible" , end = "")
else :
for i in range (n):
if ( int (i / k) % 2 = = 0 ):
print (i + k + 1 , end = " " )
else :
print (i - k + 1 , end = " " )
print ( "\n" , end = "")
if __name__ = = '__main__' :
n = 6
k = 3
kDifferencePermutation(n, k)
n = 6
k = 2
kDifferencePermutation(n, k)
n = 8
k = 2
kDifferencePermutation(n, k)
|
C#
using System;
class GFG {
static void kDifferencePermutation( int n,
int k)
{
if (!(k > 0))
{
for ( int i = 0; i < n; ++i)
Console.Write( i + 1 + " " );
}
else if (n % (2 * k) != 0)
Console.Write( "Not Possible" );
else
{
for ( int i = 0; i < n; ++i)
{
if ((i / k) % 2 == 0)
Console.Write( i + k
+ 1 + " " );
else
Console.Write( i - k
+ 1 + " " );
}
}
Console.WriteLine() ;
}
static public void Main ()
{
int n = 6 , k = 3;
kDifferencePermutation(n, k);
n = 6 ;
k = 2;
kDifferencePermutation(n, k);
n = 8 ;
k = 2;
kDifferencePermutation(n, k);
}
}
|
PHP
<?php
function kDifferencePermutation( $n , $k )
{
if (! $k )
{
for ( $i = 0; $i < $n ; ++ $i )
echo $i + 1 , " " ;
}
else if ( $n % (2 * $k ) != 0)
echo "Not Possible" ;
else
{
for ( $i = 0; $i < $n ; ++ $i )
{
if (( $i / $k ) % 2 == 0)
echo $i + $k + 1 , " " ;
else
echo $i - $k + 1 , " " ;
}
}
echo "\n" ;
}
$n = 6 ; $k = 3;
kDifferencePermutation( $n , $k );
$n = 6 ; $k = 2;
kDifferencePermutation( $n , $k );
$n = 8 ; $k = 2;
kDifferencePermutation( $n , $k );
?>
|
Javascript
<script>
function kDifferencePermutation(n, k)
{
if (!(k > 0))
{
for (let i = 0; i < n; ++i)
document.write( i + 1 + " " );
}
else if (n % (2 * k) != 0)
document.write( "Not Possible" );
else
{
for (let i = 0; i < n; ++i)
{
if (parseInt(i / k, 10) % 2 == 0)
document.write( i + k
+ 1 + " " );
else
document.write( i - k
+ 1 + " " );
}
}
document.write( "</br>" ) ;
}
let n = 6 , k = 3;
kDifferencePermutation(n, k);
n = 6 ;
k = 2;
kDifferencePermutation(n, k);
n = 8 ;
k = 2;
kDifferencePermutation(n, k);
</script>
|
Output:
4 5 6 1 2 3
Not Possible
3 4 1 2 7 8 5 6
Time complexity: O(n)
Auxiliary space: O(1)
Last Updated :
06 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...