Given two positive integers N and K, the task is to construct a permutation of the first N natural numbers such that all possible absolute differences between adjacent elements is K.
Examples:
Input: N = 3, K = 1
Output: 1 2 3
Explanation: Considering the permutation {1, 2, 3}, all possible unique absolute difference of adjacent elements is {1}. Since the count is 1(= K), print the sequence {1, 2, 3} as the resultant permutation.
Input: N = 3, K = 2
Output: 1 3 2
Naive Approach: The simplest approach to solve the given problem is to create an array with elements from 1 to N arranged in ascending order and then traverse the first K elements of the array and reverse the subarray starting at the current index and ending at the last index. After completing the above steps, print the resultant array obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void reverse( int list[], int start, int end)
{
while (start < end)
{
int temp = list[start];
list[start] = list[end];
list[end] = temp;
start++;
end--;
}
}
void makeList( int N, int K)
{
int list[N];
for ( int i = 1; i <= N; i++)
{
list[i - 1] = i;
}
for ( int i = 1; i < K; i++)
{
reverse(list, i, N - 1);
}
for ( int i = 0; i < N; i++)
{
cout << list[i] << " " ;
}
}
int main()
{
int N = 6, K = 3;
makeList(N, K);
return 0;
}
|
Java
class GFG {
public static void makeList( int N, int K)
{
int [] list = new int [N];
for ( int i = 1 ; i <= N; i++) {
list[i - 1 ] = i;
}
for ( int i = 1 ; i < K; i++) {
reverse(list, i, N - 1 );
}
for ( int i = 0 ;
i < list.length; i++) {
System.out.print(list[i] + " " );
}
}
public static void reverse(
int [] list, int start, int end)
{
while (start < end) {
int temp = list[start];
list[start] = list[end];
list[end] = temp;
start++;
end--;
}
}
public static void main(
String[] args)
{
int N = 6 , K = 3 ;
makeList(N, K);
}
}
|
Python3
def reverse(lst, start, end):
while (start < end):
temp = lst[start]
lst[start] = lst[end]
lst[end] = temp
start + = 1
end - = 1
def makelst(N, K):
lst = [ 0 for i in range (N)]
for i in range ( 1 , N + 1 , 1 ):
lst[i - 1 ] = i
for i in range ( 1 , K, 1 ):
reverse(lst, i, N - 1 )
for i in range (N):
print (lst[i], end = " " )
if __name__ = = '__main__' :
N = 6
K = 3
makelst(N, K)
|
C#
using System;
class GFG{
public static void makeList( int N, int K)
{
int [] list = new int [N];
for ( int i = 1; i <= N; i++)
{
list[i - 1] = i;
}
for ( int i = 1; i < K; i++)
{
reverse(list, i, N - 1);
}
for ( int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " " );
}
}
public static void reverse( int [] list, int start,
int end)
{
while (start < end)
{
int temp = list[start];
list[start] = list[end];
list[end] = temp;
start++;
end--;
}
}
static public void Main()
{
int N = 6, K = 3;
makeList(N, K);
}
}
|
Javascript
<script>
function makeList(N, K)
{
let list = Array.from(Array(N), ()=>Array(0));
for (let i = 1; i <= N; i++) {
list[i - 1] = i;
}
for (let i = 1; i < K; i++) {
reverse(list, i, N - 1);
}
for (let i = 0;
i < list.length; i++) {
document.write(list[i] + " " );
}
}
function reverse(
list, start, end)
{
while (start < end) {
let temp = list[start];
list[start] = list[end];
list[end] = temp;
start++;
end--;
}
}
let N = 6, K = 3;
makeList(N, K);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using the two-pointer approach. Follow the steps below to solve the problem:
- Initialize an array ans[] of size N, that stores the resultant permutation.
- Create two variables, say left and right as 1 and N respectively.
- Traverse the given array and perform the following steps:
- If the value of K is even, then push the value of the left to the array ans[] and increment the value of left by 1.
- If the value of K is odd, then push the value of the right to the array ans[] and decrement the value of right by 1.
- If the value of K is greater than 1, then decrement the value of K by 1.
- After completing the above steps, print the array ans[].
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void makeList( int N, int K)
{
int list[N];
int left = 1;
int right = N;
for ( int i = 0; i < N; i++) {
if (K % 2 == 0) {
list[i] = left;
left = left + 1;
}
else {
list[i] = right;
right = right - 1;
}
if (K > 1)
K--;
}
for ( int i = 0; i < N; i++) {
cout<<list[i]<< " " ;
}
}
int main()
{
int N = 6;
int K = 3;
makeList(N, K);
}
|
Java
class GFG {
public static void makeList( int N,
int K)
{
int [] list = new int [N];
int left = 1 ;
int right = N;
for ( int i = 0 ; i < N; i++) {
if (K % 2 == 0 ) {
list[i] = left;
left = left + 1 ;
}
else {
list[i] = right;
right = right - 1 ;
}
if (K > 1 )
K--;
}
for ( int i = 0 ;
i < list.length; i++) {
System.out.print(
list[i] + " " );
}
}
public static void main(String[] args)
{
int N = 6 ;
int K = 3 ;
makeList(N, K);
}
}
|
Python3
def makelst(N, K):
lst = [ 0 for i in range (N)]
left = 1
right = N
for i in range (N):
if (K % 2 = = 0 ):
lst[i] = left
left = left + 1
else :
lst[i] = right
right = right - 1
if (K > 1 ):
K - = 1
for i in range (N):
print (lst[i], end = " " )
if __name__ = = '__main__' :
N = 6
K = 3
makelst(N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void makeList( int N, int K)
{
int [] list = new int [N];
int left = 1;
int right = N;
for ( int i = 0; i < N; i++)
{
if (K % 2 == 0)
{
list[i] = left;
left = left + 1;
}
else
{
list[i] = right;
right = right - 1;
}
if (K > 1)
K--;
}
for ( int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " " );
}
}
public static void Main(String[] args)
{
int N = 6;
int K = 3;
makeList(N, K);
}
}
|
Javascript
<script>
function makeList(N, K)
{
var list= new Array(N);
var left = 1;
var right = N;
for ( var i = 0; i < N; i++)
{
if (K % 2 == 0)
{
list[i] = left;
left = left + 1;
}
else
{
list[i] = right;
right = right - 1;
}
if (K > 1)
K--;
}
for ( var i = 0; i < N; i++)
{
document.write(list[i] + " " );
}
}
var N = 6;
var K = 3;
makeList(N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Jun, 2021
Like Article
Save Article