Find permutation such that adjacent elements has difference of at least 2
Last Updated :
11 Oct, 2022
Given a number N, the task is to find a permutation A[] of first N integers such that the absolute difference of adjacent elements is at least 2 i.e., | Ai+1 ? Ai | ? 2 for all 0 ? i < N?1.If no such permutation exists, print -1.
Examples:
Input: N = 4
Output: 3 1 4 2
?Explanation: Here A[] = {3, 1, 4, 2} satisfies the given condition.
Since | Ai+1 ? Ai | ? 2 for all 0 ? i < N?1
Input: N = 2
Output: -1
Explanation: No such permutation is possible that satisfies the given condition
Approach: The problem can be solved based on the following observation:
- If N = 2 or N = 3, then no array exists that satisfy the above condition.
- Otherwise, array exists that satisfy the above condition such as:
- First print all odd numbers from N to 1 in decreasing order and after that print all even numbers in decreasing order.
Follow the steps mentioned below to implement the idea:
- If N = 2 or N = 3, print -1.
- Otherwise, check whether N is odd or even:
- If N is odd, iterate a loop from N to 1 to print all odd numbers after that iterate another loop from N – 1 to 2 to print even numbers.
- If N is even, iterate a loop from N – 1 to 1 to print all odd numbers after that iterate another loop from N to 2 to print even numbers.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findArray( int n)
{
if (n == 2 || n == 3)
cout << -1;
else {
if ((n % 2) == 1) {
for ( int i = n; i >= 1; i -= 2) {
cout << i << " " ;
}
for ( int i = n - 1; i >= 2; i -= 2) {
cout << i << " " ;
}
}
else {
for ( int i = n - 1; i >= 1; i -= 2) {
cout << i << " " ;
}
for ( int i = n; i >= 2; i -= 2) {
cout << i << " " ;
}
}
}
}
int main()
{
int N = 4;
findArray(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void findArray( int n)
{
if (n == 2 || n == 3 )
System.out.println(- 1 );
else {
if ((n % 2 ) == 1 ) {
for ( int i = n; i >= 1 ; i -= 2 ) {
System.out.print(i + " " );
}
for ( int i = n - 1 ; i >= 2 ; i -= 2 ) {
System.out.print(i + " " );
}
}
else {
for ( int i = n - 1 ; i >= 1 ; i -= 2 ) {
System.out.print(i + " " );
}
for ( int i = n; i >= 2 ; i -= 2 ) {
System.out.print(i + " " );
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 4 ;
findArray(N);
}
}
|
Python3
def findArray(n):
if (n = = 2 or n = = 3 ) :
print ( - 1 );
else :
if ((n % 2 ) = = 1 ) :
for i in range (n, 0 , - 2 ) :
print (i,end = " " );
for i in range (n - 1 , 1 , - 2 ) :
print (i,end = " " );
else :
for i in range ( n - 1 , 0 , - 2 ) :
print (i,end = " " );
for i in range (n, 1 , - 2 ) :
print (i, end = " " );
if __name__ = = "__main__" :
N = 4 ;
findArray(N);
|
C#
using System;
public class GFG {
static void findArray( int n)
{
if (n == 2 || n == 3)
Console.WriteLine(-1);
else {
if ((n % 2) == 1) {
for ( int i = n; i >= 1; i -= 2) {
Console.Write(i + " " );
}
for ( int i = n - 1; i >= 2; i -= 2) {
Console.Write(i + " " );
}
}
else {
for ( int i = n - 1; i >= 1; i -= 2) {
Console.Write(i + " " );
}
for ( int i = n; i >= 2; i -= 2) {
Console.Write(i + " " );
}
}
Console.WriteLine();
}
}
static public void Main()
{
int N = 4;
findArray(N);
}
}
|
Javascript
function findArray(n)
{
if (n == 2 || n == 3)
process.stdout.write(-1);
else {
if ((n % 2) == 1) {
for (let i = n; i >= 1; i -= 2) {
process.stdout.write(i + " " );
}
for (let i = n - 1; i >= 2; i -= 2) {
process.stdout.write(i + " " );
}
}
else {
for (let i = n - 1; i >= 1; i -= 2) {
process.stdout.write(i + " " );
}
for (let i = n; i >= 2; i -= 2) {
process.stdout.write(i + " " );
}
}
}
}
let N = 4;
findArray(N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...