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 are 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
The naive approach and the two-pointer approach of this problem are already discussed here. This article discusses a different approach deque.
Approach: It is easy to see that, answers for all values of K between [1, N-1] can be generated. For any K outside this range, there exists no answer. To solve the problem maintain a double-ended queue for all the current elements and a vector to store the sequence. Also, maintain a boolean value that will help to determine to pop the front or back element. Iterate the remaining element and if K is greater than 1 then push the element according to the boolean value and decrease K by 1. Flip the boolean value so that all remaining differences will have value 1. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void K_ValuesArray( int N, int K)
{
if (K < 1 || K >= N) {
cout << -1;
return ;
}
deque< int > dq;
for ( int i = 2; i <= N; i++) {
dq.push_back(i);
}
bool front = true ;
vector< int > ans;
ans.push_back(1);
if (K > 1) {
front ^= 1;
K--;
}
for ( int i = 2; i <= N; i++) {
if (front) {
int val = dq.front();
dq.pop_front();
ans.push_back(val);
if (K > 1) {
K--;
front ^= 1;
}
}
else {
int val = dq.back();
dq.pop_back();
ans.push_back(val);
if (K > 1) {
K--;
front ^= 1;
}
}
}
for ( int i = 0; i < N; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
int N = 7, K = 1;
K_ValuesArray(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void K_ValuesArray( int N, int K)
{
if (K < 1 || K >= N) {
System.out.print(- 1 );
return ;
}
Deque<Integer> dq = new LinkedList<Integer>();
for ( int i = 2 ; i <= N; i++) {
dq.add(i);
}
boolean front = true ;
Vector<Integer> ans = new Vector<Integer>();
ans.add( 1 );
if (K > 1 ) {
front ^= true ;
K--;
}
for ( int i = 2 ; i <= N; i++) {
if (front) {
int val = dq.peek();
dq.removeFirst();
ans.add(val);
if (K > 1 ) {
K--;
front ^= true ;
}
}
else {
int val = dq.getLast();
dq.removeLast();
ans.add(val);
if (K > 1 ) {
K--;
front ^= true ;
}
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(ans.get(i)+ " " );
}
}
public static void main(String[] args)
{
int N = 7 , K = 1 ;
K_ValuesArray(N, K);
}
}
|
Python3
from collections import deque
def K_ValuesArray(N, K):
if (K < 1 or K > = N):
print ( "-1" )
return
dq = deque()
for i in range ( 2 , N + 1 ):
dq.append(i)
front = True
ans = []
ans.append( 1 )
if (K > 1 ):
front ^ = 1
K - = 1
for i in range ( 2 , N + 1 ):
if (front):
val = dq.popleft()
ans.append(val)
if (K > 1 ):
K - = 1
front ^ = 1
else :
val = dq.pop()
ans.append(val)
if (K > 1 ):
K - = 1
front ^ = 1
for i in range ( 0 , N):
print (ans[i], end = " " )
if __name__ = = "__main__" :
N = 7
K = 1
K_ValuesArray(N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void K_ValuesArray( int N, int K)
{
if (K < 1 || K >= N)
{
Console.Write(-1);
return ;
}
LinkedList< int > dq = new LinkedList< int >();
for ( int i = 2; i <= N; i++)
{
dq.AddLast(i);
}
bool front = true ;
List< int > ans = new List< int >();
ans.Add(1);
if (K > 1)
{
front ^= true ;
K--;
}
for ( int i = 2; i <= N; i++)
{
if (front)
{
int val = dq.First.Value;
dq.RemoveFirst();
ans.Add(val);
if (K > 1)
{
K--;
front ^= true ;
}
}
else
{
int val = dq.Last.Value;
dq.RemoveLast();
ans.Add(val);
if (K > 1)
{
K--;
front ^= true ;
}
}
}
for ( int i = 0; i < N; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main()
{
int N = 7, K = 1;
K_ValuesArray(N, K);
}
}
|
Javascript
<script>
function K_ValuesArray(N, K) {
if (K < 1 || K >= N) {
document.write(-1);
return ;
}
let dq = new Array();
for (let i = 2; i <= N; i++) {
dq.push(i);
}
let front = true ;
let ans = new Array();
ans.push(1);
if (K > 1) {
front ^= true ;
K--;
}
for (let i = 2; i <= N; i++) {
if (front) {
let val = dq[0];
dq.shift();
ans.push(val);
if (K > 1) {
K--;
front ^= true ;
}
}
else {
let val = dq.Last.Value;
dq.pop();
ans.push(val);
if (K > 1) {
K--;
front ^= true ;
}
}
}
for (let i = 0; i < N; i++) {
document.write(ans[i] + " " );
}
}
let N = 7, K = 1;
K_ValuesArray(N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)