# Arrange first N natural numbers such that absolute difference between all adjacent elements > 1

• Last Updated : 21 Apr, 2021

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 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:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to print the required permutation``void` `arrange(``int` `N)``{` `    ``if` `(N == 1) {``        ``cout << ``"1"``;``        ``return``;``    ``}` `    ``// No permutation is possible``    ``// satisfying the given condition``    ``if` `(N == 2 || N == 3) {``        ``cout << ``"-1"``;``        ``return``;``    ``}` `    ``// Maximum even and odd elements < N``    ``int` `even = -1, odd = -1;``    ``if` `(N % 2 == 0) {``        ``even = N;``        ``odd = N - 1;``    ``}``    ``else` `{``        ``odd = N;``        ``even = N - 1;``    ``}` `    ``// Print all odd elements in decreasing order``    ``while` `(odd >= 1) {``        ``cout << odd << ``" "``;` `        ``// Next element must be odd``        ``odd = odd - 2;``    ``}` `    ``// Print all even elements in decreasing order``    ``while` `(even >= 2) {``        ``cout << even << ``" "``;` `        ``// Next element must be even``        ``even = even - 2;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `N = 5;``    ``arrange(N);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{` `// Function to print the required``// permutation``static` `void` `arrange(``int` `N)``{``    ``if` `(N == ``1``)``    ``{``        ``System.out.println(``"1"``);``        ``return``;``    ``}` `    ``// No permutation is possible``    ``// satisfying the given condition``    ``if` `(N == ``2` `|| N == ``3``)``    ``{``        ``System.out.println(``"-1"``);``        ``return``;``    ``}` `    ``// Maximum even and odd elements < N``    ``int` `even = -``1``, odd = -``1``;``    ``if` `(N % ``2` `== ``0``)``    ``{``        ``even = N;``        ``odd = N - ``1``;``    ``}``    ``else``    ``{``        ``odd = N;``        ``even = N - ``1``;``    ``}` `    ``// Print all odd elements in``    ``// decreasing order``    ``while` `(odd >= ``1``)``    ``{``        ``System.out.print(odd);``        ``System.out.print(``" "``);``    ` `        ``// Next element must be odd``        ``odd = odd - ``2``;``    ``}` `    ``// Print all even elements in``    ``// decreasing order``    ``while` `(even >= ``2``)``    ``{``        ``System.out.print(even);``        ``System.out.print(``" "``);` `        ``// Next element must be even``        ``even = even - ``2``;``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``5``;``    ``arrange(N);``}``}` `// This code is contributed``// by Akanksha Rai`

## Python3

 `# Python3 implementation of the approach` `# Function to print the required permutation``def` `arrange(N):` `    ``if` `(N ``=``=` `1``) :``        ``print``(``"1"``)``        ``return` `    ``# No permutation is possible``    ``# satisfying the given condition``    ``if` `(N ``=``=` `2` `or` `N ``=``=` `3``) :``        ``print``(``"-1"``)``        ``return` `    ``# Maximum even and odd elements < N``    ``even ``=` `-``1``    ``odd ``=` `-``1``    ``if` `(N ``%` `2` `=``=` `0``):``        ``even ``=` `N``        ``odd ``=` `N ``-` `1``    ``else` `:``        ``odd ``=` `N``        ``even ``=` `N ``-` `1` `    ``# Print all odd elements in``    ``# decreasing order``    ``while` `(odd >``=` `1``):``        ``print``(odd, end ``=` `" "``)` `        ``# Next element must be odd``        ``odd ``=` `odd ``-` `2` `    ``# Print all even elements in``    ``# decreasing order``    ``while` `(even >``=` `2``):``        ``print``(even, end ``=` `" "``)` `        ``# Next element must be even``        ``even ``=` `even ``-` `2` `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `5``    ``arrange(N)` `# This code is contributed by ita_c`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `// Function to print the required``// permutation``static` `void` `arrange(``int` `N)``{``    ``if` `(N == 1)``    ``{``        ``Console.WriteLine(``"1"``);``        ``return``;``    ``}` `    ``// No permutation is possible``    ``// satisfying the given condition``    ``if` `(N == 2 || N == 3)``    ``{``        ``Console.WriteLine(``"-1"``);``        ``return``;``    ``}` `    ``// Maximum even and odd elements < N``    ``int` `even = -1, odd = -1;``    ``if` `(N % 2 == 0)``    ``{``        ``even = N;``        ``odd = N - 1;``    ``}``    ``else``    ``{``        ``odd = N;``        ``even = N - 1;``    ``}` `    ``// Print all odd elements in``    ``// decreasing order``    ``while` `(odd >= 1)``    ``{``        ``Console.Write(odd);``        ``Console.Write(``" "``);``    ` `        ``// Next element must be odd``        ``odd = odd - 2;``    ``}` `    ``// Print all even elements in``    ``// decreasing order``    ``while` `(even >= 2)``    ``{``        ``Console.Write(even);``        ``Console.Write(``" "``);` `        ``// Next element must be even``        ``even = even - 2;``    ``}``}` `// Driver code``public` `static` `void` `Main()``{``    ``int` `N = 5;``    ``arrange(N);``}``}` `// This code is contributed``// by Shivi_Aggarwal`

## PHP

 `= 1)``    ``{``        ``echo` `\$odd``, ``" "``;` `        ``// Next element must be odd``        ``\$odd` `= ``\$odd` `- 2;``    ``}` `    ``// Print all even elements in``    ``// decreasing order``    ``while` `(``\$even` `>= 2)``    ``{``        ``echo` `\$even``, ``" "``;` `        ``// Next element must be even``        ``\$even` `= ``\$even` `- 2;``    ``}``}` `// Driver code``\$N` `= 5;``arrange(``\$N``);` `// This code is contributed by Ryuga``?>`

## Javascript

 ``
Output:
`5 3 1 4 2`

My Personal Notes arrow_drop_up