Generate an N-length permutation such that absolute difference between adjacent elements are present in the range [2, 4] Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report 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:Consider a permutation {3, 1, 4, 2}. Now, the absolute difference between adjacent elements are {2, 3, 2}. Input: N = 9Output: 9 7 5 3 1 4 2 6 8 Approach: The given problem can be solved by grouping up consecutive even and odd elements together to construct the permutation. Follow the steps below to solve the problem: If the value of N is less than 4 then print -1 as it is impossible to construct a permutation according to the given conditions for N less than 4.Initialize a variable, say i as N, and perform the following steps below:If the value of i is even, then decrement the value of i by 1.Iterate until the value of i is at least 1 and print the value of i and decrement the value of i by 2.Print 4 and 2 and update the value of i to 6.Iterate in the range [i, N] and print the value of i and increment the value of i by 2. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the permutation of // size N with absolute difference of // adjacent elements in range [2, 4] void getPermutation(int N) { // If N is less than 4 if (N <= 3) { cout << -1; return; } int i = N; // Check if N is even if (N % 2 == 0) i--; // Traverse through odd integers while (i >= 1) { cout << i << " "; i -= 2; } cout << 4 << " " << 2 << " "; // Update the value of i i = 6; // Traverse through even integers while (i <= N) { cout << i << " "; i += 2; } } // Driver Code int main() { int N = 9; getPermutation(N); return 0; } Java // Java program for the above approach import java.util.*; class GFG{ // Function to print the permutation of // size N with absolute difference of // adjacent elements in range [2, 4] static void getPermutation(int N) { // If N is less than 4 if (N <= 3) { System.out.print(-1); return; } int i = N; // Check if N is even if (N % 2 == 0) i--; // Traverse through odd integers while (i >= 1) { System.out.print(i + " "); i -= 2; } System.out.print(4 + " " + 2 +" "); // Update the value of i i = 6; // Traverse through even integers while (i <= N) { System.out.print(i + " "); i += 2; } } // Driver Code public static void main(String[] args) { int N = 9; getPermutation(N); } } // This code is contributed by sanjoy_62 Python3 # Python3 program for the above approach # Function to print permutation of # size N with absolute difference of # adjacent elements in range [2, 4] def getPermutation(N): # If N is less than 4 if (N <= 3): print(-1) return i = N # Check if N is even if (N % 2 == 0): i -= 1 # Traverse through odd integers while (i >= 1): print(i, end = " ") i -= 2 print(4, 2, end = " ") # Update the value of i i = 6 # Traverse through even integers while (i <= N): print( i, end = " ") i += 2 # Driver Code if __name__ == '__main__': N = 9 getPermutation(N) # This code is contributed by mohit kumar 29. C# // C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to print the permutation of // size N with absolute difference of // adjacent elements in range [2, 4] static void getPermutation(int N) { // If N is less than 4 if (N <= 3) { Console.Write(-1); return; } int i = N; // Check if N is even if (N % 2 == 0) i--; // Traverse through odd integers while (i >= 1) { Console.Write(i + " "); i -= 2; } Console.Write(4 + " " + 2 +" "); // Update the value of i i = 6; // Traverse through even integers while (i <= N) { Console.Write(i +" "); i += 2; } } // Driver Code public static void Main() { int N = 9; getPermutation(N); } } // This code is contributed by SURENDRA_GANGWAR JavaScript <script> // JavaScript program for the above approach // Function to print the permutation of // size N with absolute difference of // adjacent elements in range [2, 4] function getPermutation(N) { // If N is less than 4 if (N <= 3) { document.write(-1); return; } let i = N; // Check if N is even if (N % 2 == 0) i--; // Traverse through odd integers while (i >= 1) { document.write(i + " "); i -= 2; } document.write(4 + " " + 2 + " "); // Update the value of i i = 6; // Traverse through even integers while (i <= N) { document.write(i + " "); i += 2; } } // Driver Code let N = 9; getPermutation(N); // This code is contributed by Potta Lokesh </script> Output: 9 7 5 3 1 4 2 6 8 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Arrange first N natural numbers such that absolute difference between all adjacent elements > 1 P patelneel3333 Follow Improve Article Tags : DSA permutation Natural Numbers Practice Tags : permutation Similar Reads Arrange first N natural numbers such that absolute difference between all adjacent elements > 1 Given an integer N. The task is to find the permutation of first N natural numbers such that the absolute difference between any two consecutive numbers > 1. If no such permutation is possible then print -1. Examples: Input: N = 5 Output: 5 3 1 4 2 Input: N = 3 Output: -1 Approach: There may be m 6 min read Number of possible permutations when absolute difference between number of elements to the right and left are given Given an array of N elements where each element i, the absolute difference between total elements to the right and left of it are given. Find the number of possible ordering of the actual array elements.Examples: Input : N = 5, arr[] = {2, 4, 4, 0, 2} Output : 4 There are four possible orders, as fo 10 min read 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 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 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 Count of Arrays of size N having absolute difference between adjacent elements at most 1 Given a positive integer M and an array arr[] of size N and a few integers are missing in the array represented as -1, the task is to find the count of distinct arrays after replacing all -1 with the elements over the range [1, M] such that the absolute difference between any pair of adjacent elemen 15+ min read Like