Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
Given two integers N and K where K < N, the task is to generate a permutation of integers from 1 to N such that the absolute difference of all the consecutive integers give exactly K distinct integers.
Examples:
Input: N = 3, K = 2
Output: 1 3 2
|1 – 3| = 2 and |3 – 2| = 1 which gives 2 distinct integers (2 and 1)
Input: N = 5, K = 4
Output: 1 5 2 4 3
|1 – 5| = 4, |5 – 2| = 3, |2 – 4| = 2 and |4 – 3| = 1 gives 4 distinct integers i.e. 4, 3, 2 and 1
Approach: The problem can be easily solved by simple observation. At the odd indices place increasing sequence 1, 2, 3, … and at the even indices place the decreasing sequence N, N-1, N-2, … and so on.
For N = 10, a permutation with distinct integers for consecutive absolute difference can be 1 10 2 9 3 8 4 7 5 6. The consecutive absolute difference gives integers 9, 8, 7 and so on.
So, first print K integers of such a sequence then make the rest of the differences equal to 1. The code is quite self explanatory.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPermutation( int N, int K)
{
vector< int > res;
int l = 1, r = N, flag = 0;
for ( int i = 0; i < K; i++) {
if (!flag) {
res.push_back(l);
l++;
}
else {
res.push_back(r);
r--;
}
flag ^= 1;
}
if (!flag) {
for ( int i = r; i >= l; i--)
res.push_back(i);
}
else {
for ( int i = l; i <= r; i++)
res.push_back(i);
}
for ( auto i : res)
cout << i << " " ;
}
int main()
{
int N = 10, K = 4;
printPermutation(N, K);
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static void printPermutation( int N, int K)
{
Vector<Integer> res = new Vector<>();
int l = 1 , r = N, flag = 0 ;
for ( int i = 0 ; i < K; i++)
{
if (flag == 0 )
{
res.add(l);
l++;
}
else
{
res.add(r);
r--;
}
flag ^= 1 ;
}
if (flag != 1 )
{
for ( int i = r; i >= l; i--)
{
res.add(i);
}
}
else
{
for ( int i = l; i <= r; i++)
{
res.add(i);
}
}
for (Integer i : res)
{
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int N = 10 , K = 4 ;
printPermutation(N, K);
}
}
|
Python3
def printPermutation(N, K):
res = list ();
l, r, flag = 1 , N, 0
for i in range (K):
if flag = = False :
res.append(l)
l + = 1
else :
res.append(r);
r - = 1 ;
flag = flag ^ 1 ;
if flag = = False :
for i in range (r, 2 , - 1 ):
res.append(i)
else :
for i in range (l, r):
res.append(i)
for i in res:
print (i, end = " " )
N, K = 10 , 4
printPermutation(N, K)
|
C#
using System;
using System.Collections;
class GFG
{
static void printPermutation( int N, int K)
{
ArrayList res = new ArrayList();
int l = 1, r = N, flag = 0;
for ( int i = 0; i < K; i++)
{
if (flag == 0)
{
res.Add(l);
l++;
}
else
{
res.Add(r);
r--;
}
flag ^= 1;
}
if (flag != 1)
{
for ( int i = r; i >= l; i--)
{
res.Add(i);
}
}
else
{
for ( int i = l; i <= r; i++)
{
res.Add(i);
}
}
foreach ( int i in res)
{
Console.Write(i + " " );
}
}
public static void Main()
{
int N = 10, K = 4;
printPermutation(N, K);
}
}
|
PHP
<?php
function printPermutation( $N , $K )
{
$res = array ();
$l = 1;
$r = $N ;
$flag = 0;
for ( $i = 0; $i < $K ; $i ++)
{
if (! $flag )
{
array_push ( $res , $l );
$l ++;
}
else
{
array_push ( $res , $r );
$r --;
}
$flag ^= 1;
}
if (! $flag )
{
for ( $i = $r ; $i >= $l ; $i --)
array_push ( $res , $i );
}
else
{
for ( $i = l; $i <= $r ; $i ++)
array_push ( $res , $i );
}
for ( $i = 0; $i < sizeof( $res ); $i ++)
echo $res [ $i ], " " ;
}
$N = 10;
$K = 4;
printPermutation( $N , $K );
?>
|
Javascript
<script>
function printPermutation(N, K)
{
var res = [];
var l = 1, r = N, flag = 0;
for ( var i = 0; i < K; i++)
{
if (!flag)
{
res.push(l);
l++;
}
else
{
res.push(r);
r--;
}
flag ^= 1;
}
if (!flag)
{
for ( var i = r; i >= l; i--)
res.push(i);
}
else
{
for ( var i = l; i <= r; i++)
res.push(i);
}
for ( var i = 0; i< res.length; i++)
{
document.write(res[i] + " " );
}
}
var N = 10, K = 4;
printPermutation(N, K);
</script>
|
Output:
1 10 2 9 8 7 6 5 4 3
Time Complexity : O(K+N)
Space Complexity : O(N)
Last Updated :
08 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...