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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to print the required 
// permutation 
function arrange($N
    if ($N == 1) 
    
        echo "1"
        return
    
  
    // No permutation is possible 
    // satisfying the given condition 
    if ($N == 2 || $N == 3)
    
        echo "-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) 
    
        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
?>

chevron_right


Output:

5 3 1 4 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.