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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the array
void findArray(int n)
{
if (n == 2 || n == 3)
cout << -1;
else {
// If n is odd
if ((n % 2) == 1) {
// Loops to print the permutation
for (int i = n; i >= 1; i -= 2) {
cout << i << " ";
}
for (int i = n - 1; i >= 2; i -= 2) {
cout << i << " ";
}
}
// If n is even
else {
// Loops to print the permutation
for (int i = n - 1; i >= 1; i -= 2) {
cout << i << " ";
}
for (int i = n; i >= 2; i -= 2) {
cout << i << " ";
}
}
}
}
// Driver Code
int main()
{
int N = 4;
// Function call
findArray(N);
return 0;
}
// This code is contributed by aarohirai2616.
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find the array
public static void findArray(int n)
{
if (n == 2 || n == 3)
System.out.println(-1);
else {
// If n is odd
if ((n % 2) == 1) {
// Loops to print the permutation
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 + " ");
}
}
// If n is even
else {
// Loop to print the permutation
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();
}
}
// Driver code
public static void main(String[] args)
{
int N = 4;
// Function call
findArray(N);
}
}
Python3
# Python3 code to implement the approach
# Function to find the array
def findArray(n):
if (n == 2 or n == 3) :
print(-1);
else :
# If n is odd
if ((n % 2) == 1) :
# Loops to print the permutation
for i in range(n, 0, -2) :
print(i,end=" ");
for i in range(n - 1, 1, -2) :
print(i,end = " ");
# If n is even
else :
# Loops to print the permutation
for i in range( n - 1, 0, -2) :
print(i,end=" ");
for i in range(n, 1, -2) :
print(i, end=" ");
# Driver Code
if __name__ == "__main__" :
N = 4;
# Function call
findArray(N);
# This code is contributed by AnkThon
C#
// C# code to implement the approach
using System;
public class GFG {
// Function to find the array
static void findArray(int n)
{
if (n == 2 || n == 3)
Console.WriteLine(-1);
else {
// If n is odd
if ((n % 2) == 1) {
// Loops to print the permutation
for (int i = n; i >= 1; i -= 2) {
Console.Write(i + " ");
}
for (int i = n - 1; i >= 2; i -= 2) {
Console.Write(i + " ");
}
}
// If n is even
else {
// Loop to print the permutation
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()
{
// Code
int N = 4;
// Function call
findArray(N);
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// Javascript code to implement the approach
// Function to find the array
function findArray(n)
{
if (n == 2 || n == 3)
process.stdout.write(-1);
else {
// If n is odd
if ((n % 2) == 1) {
// Loops to print the permutation
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 + " ");
}
}
// If n is even
else {
// Loop to print the permutation
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 + " ");
}
}
}
}
// Driver code
let N = 4;
// Function call
findArray(N);
// This code is contributed by aarohirai2616.
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Generate a Permutation of 1 to N with no adjacent elements difference as 1 Given an integer N, the task is to construct a permutation from 1 to N where no adjacent elements have difference as 1. If there is no such permutation, print -1. Permutation from 1 to N has all the numbers from 1 to N present exactly once. Examples: Input: N = 5 Output: 4 2 5 3 1Explanation: As for
5 min read
Longest Subsequence such that difference between adjacent elements is either A or B Given an array arr of size N, and two integers A and B. The task is to find the length of the longest subsequence with the difference between adjacent elements as either A or B. Example: Input : arr[]={ 5, 5, 5, 10, 8, 6, 12, 13 }, A=0, B=1Output : 4Explanation : Maximum length subsequence is {5,5,5
6 min read
Print longest Subsequence such that difference between adjacent elements is K Given an array arr[] of size N, and integer K. The task is to find the longest subsequence with the difference between adjacent elements as K Examples: Input: arr[] = { 5, 5, 5, 10, 8, 6, 12, 13 }, K = 1Output: {5, 6} Input: arr[] = {4, 6, 7, 8, 9, 8, 12, 14, 17, 15}, K = 2Output: {4, 6, 8} Approach
9 min read
Permutation of first N elements with absolute adjacent difference in increasing order Given a positive integer N, the task is to construct a permutation from 1 to N such that the absolute difference of elements is in strictly increasing order. Note: N cannot be 0 or 1. Examples: Input: N = 10Output: 6 5 7 4 8 3 9 2 10 1Explanation: abs(6 - 5) i.e., 1 < abs(5 - 7) i.e., 2 < abs(
5 min read
Rearrange array such that difference of adjacent elements is in descending order Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output : 6 1 5 2 4 3 Explanation: For first two elements the difference is abs(6-1)=5
6 min read
Permutation of Array such that products of all adjacent elements are even Given an array arr[] consisting of N positive integers, the task is to find any permutation of given array such that the product of adjacent elements is even. Print any such permutation or -1 if not possible. Example: Input: arr[] = {6,7,9,8,10,11}Output: 8 9 10 7 6 11Explanation: Product of adjacen
7 min read