Generate a Permutation of 1 to N with no adjacent elements difference as 1 Last Updated : 26 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 N = 5, [ 4 2 5 3 1 ] is the only permutation where the difference between the adjacent elements is not 1. Input: N = 3Output: -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 1So, 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++ // C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the permutation // which satisfies the given condition vector<int> permutation(int n) { vector<int> ans; if (n <= 3) { ans.push_back(-1); } // If n is even 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); } } // If n is odd 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; } // Driver Code int main() { int N = 5; vector<int> ans = permutation(N); for (int x : ans) cout << x << " "; return 0; } Java // Java code for the above approach import java.util.*; class GFG { // Function to find the permutation // which satisfies the given condition static List<Integer> permutation(int n) { List<Integer> ans = new ArrayList<>(); if (n <= 3) { ans.add(-1); } // If n is even 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); } } // If n is odd 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; } // Driver Code public static void main (String[] args) { int N = 5; List<Integer> ans = permutation(N); for (Integer x : ans) System.out.print(x + " "); } } // This code is contributed by hrithikgarg03188. Python3 # Python program to generate permutation of 1 to n # with no adjacent element difference as 1 def permutation(n): # for storing the resultant permutations ans = [] if n <= 3: ans.append(-1) # if n is even if n % 2 == 0: i = 0 while i <= n: ans.append(i) i += 2 i = 1 while i < n: ans.append(i) i += 2 # if n is odd else: i = 2 while i <= n-1: ans.append(i) i += 2 j = 1 while j <= n: ans.append(j) j += 2 return ans # Driver Code if __name__ == '__main__': n = 5 ans = permutation(n) for i in ans: print(i, end=" ") # This code is contributed by Amnindersingh1414. C# // C# code for the above approach using System; using System.Collections.Generic; public class GFG { // Function to find the permutation // which satisfies the given condition static List<int> permutation(int n) { List<int> ans = new List<int>(); if (n <= 3) { ans.Add(-1); } // If n is even 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); } } // If n is odd 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; } // Driver Code public static void Main(String[] args) { int N = 5; List<int> ans = permutation(N); foreach (int x in ans) Console.Write(x + " "); } } // This code is contributed by gauravrajput1 JavaScript <script> // JavaScript code for the above approach // Function to find the permutation // which satisfies the given condition function permutation(n) { let ans=[]; if (n <= 3) { ans.push(-1); } // If n is even 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); } } // If n is odd 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; } // Driver Code let N = 5; let ans = permutation(N); for (let x of ans) document.write( x + " "); // This code is contributed by Potta Lokesh </script> Output2 4 1 3 5 Time complexity: O(N)Auxiliary Space: O(N) because it is using extra space for vector ans. Comment More infoAdvertise with us Next Article Generate an N-length permutation such that absolute difference between adjacent elements are present in the range [2, 4] M mayank007rawa Follow Improve Article Tags : Misc Pattern Searching DSA permutation Practice Tags : MiscPattern Searchingpermutation Similar Reads Generate permutation of [1, N] having bitwise XOR of adjacent differences as 0 Given an integer N, the task is to generate a permutation from 1 to N such that the bitwise XOR of differences between adjacent elements is 0 i.e., | A[1]â A[2] | ^ | A[2]â A[3] | ^ . . . ^ | A[N â1] â A[N] | = 0, where |X - Y| represents absolute difference between X and Y. Examples: Input: N = 4Ou 5 min read Find permutation such that adjacent elements has difference of at least 2 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 = 4Output: 3 1 4 2?Explanation: Here A[] = {3, 1 6 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 Generate a permutation of first N natural numbers from an array of differences between adjacent elements Given an array arr[] consisting of (N - 1), the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] - P[i]). If there exists no such permutation, then print "-1". Examples: Input: arr[] = {-1, 2, -3, -1}Output: 4 3 5 2 1Explanation:For t 8 min read Generate an N-length permutation such that absolute difference between adjacent elements are present in the range [2, 4] Given a positive integer N, the task is to construct a permutation of first N natural numbers such that the absolute difference between adjacent elements is either 2, 3, or 4. If it is not possible to construct such a permutation, then print "-1". Examples: Input: N = 4Output: 3 1 4 2Explanation:Con 5 min read Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K 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 = 1Output: 1 2 3Explanation: Considering the permutation {1, 2, 3}, all possible unique abs 11 min read Like