Generate a Permutation of 1 to N with no adjacent elements difference as 1
Last Updated :
26 Sep, 2022
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 1
Explanation: As for N = 5, [ 4 2 5 3 1 ] is the only permutation where the difference between the adjacent elements is not 1.
Input: N = 3
Output: -1
Approach: The problem can be solved based on the following idea:
- Difference between any two even number is more than 1 and similarly, for all odd elements their difference is more than 1
- So, print all the even numbers first followed by the odd numbers.
Follow the steps mentioned below to implement the above approach:
- If N is less than or equal to 3, then no such permutation is possible.
- If N is even, print all even numbers from 2 to N firstly, and then all odds from 1 to N – 1 print all odd numbers.
- If the N is odd, then print all even numbers from 2 to N – 1 and then all odd numbers from 1 to N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > permutation( int n)
{
vector< int > ans;
if (n <= 3) {
ans.push_back(-1);
}
if (n % 2 == 0) {
for ( int i = 2; i <= n; i += 2) {
ans.push_back(i);
}
for ( int i = 1; i < n; i += 2) {
ans.push_back(i);
}
}
else {
for ( int i = 2; i <= n - 1; i += 2) {
ans.push_back(i);
}
for ( int j = 1; j <= n; j += 2) {
ans.push_back(j);
}
}
return ans;
}
int main()
{
int N = 5;
vector< int > ans = permutation(N);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static List<Integer> permutation( int n)
{
List<Integer> ans = new ArrayList<>();
if (n <= 3 ) {
ans.add(- 1 );
}
if (n % 2 == 0 ) {
for ( int i = 2 ; i <= n; i += 2 ) {
ans.add(i);
}
for ( int i = 1 ; i < n; i += 2 ) {
ans.add(i);
}
}
else {
for ( int i = 2 ; i <= n - 1 ; i += 2 ) {
ans.add(i);
}
for ( int j = 1 ; j <= n; j += 2 ) {
ans.add(j);
}
}
return ans;
}
public static void main (String[] args) {
int N = 5 ;
List<Integer> ans = permutation(N);
for (Integer x : ans)
System.out.print(x + " " );
}
}
|
Python3
def permutation(n):
ans = []
if n < = 3 :
ans.append( - 1 )
if n % 2 = = 0 :
i = 0
while i < = n:
ans.append(i)
i + = 2
i = 1
while i < n:
ans.append(i)
i + = 2
else :
i = 2
while i < = n - 1 :
ans.append(i)
i + = 2
j = 1
while j < = n:
ans.append(j)
j + = 2
return ans
if __name__ = = '__main__' :
n = 5
ans = permutation(n)
for i in ans:
print (i, end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static List< int > permutation( int n) {
List< int > ans = new List< int >();
if (n <= 3) {
ans.Add(-1);
}
if (n % 2 == 0) {
for ( int i = 2; i <= n; i += 2) {
ans.Add(i);
}
for ( int i = 1; i < n; i += 2) {
ans.Add(i);
}
}
else {
for ( int i = 2; i <= n - 1; i += 2) {
ans.Add(i);
}
for ( int j = 1; j <= n; j += 2) {
ans.Add(j);
}
}
return ans;
}
public static void Main(String[] args) {
int N = 5;
List< int > ans = permutation(N);
foreach ( int x in ans)
Console.Write(x + " " );
}
}
|
Javascript
<script>
function permutation(n)
{
let ans=[];
if (n <= 3) {
ans.push(-1);
}
if (n % 2 == 0) {
for (let i = 2; i <= n; i += 2) {
ans.push(i);
}
for (let i = 1; i < n; i += 2) {
ans.push(i);
}
}
else {
for (let i = 2; i <= n - 1; i += 2) {
ans.push(i);
}
for (let j = 1; j <= n; j += 2) {
ans.push(j);
}
}
return ans;
}
let N = 5;
let ans = permutation(N);
for (let x of ans)
document.write( x + " " );
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N) because it is using extra space for vector ans.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...