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.
Input: N = 5
Output: 5 3 1 4 2
Input: N = 3
Approach: There may be many such arrangements possible but one of the most common and greedy approach is to arrange all odd numbers in decreasing (or increasing) order and after that arrange all even numbers in decreasing (or increasing) order. Note that if N = 3 or N = 2 then there will be no such arrangement possible and if N = 1 then the sequence will consist of a single element i.e. 1.
Below is the implementation of the above approach:
5 3 1 4 2
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Maximum sum of difference of adjacent elements
- Count maximum elements of an array whose absolute difference does not exceed K
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Arrange given numbers to form the smallest number
- Sort an array according to absolute difference with given value
- Minimum absolute difference between N and a power of 2
- Find the node whose absolute difference with X gives maximum value
- Find the node whose absolute difference with X gives minimum value
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Pair with minimum absolute difference after solving each query
- Rearrange numbers in an array such that no two adjacent numbers are same
- Distinct adjacent elements in an array
- Distinct adjacent elements in a binary array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.