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 2Input: 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 <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; } |
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
Python 3
# 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 |
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
<?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 ?> |
5 3 1 4 2
Recommended Posts:
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Count maximum elements of an array whose absolute difference does not exceed K
- Maximum sum of difference of adjacent elements
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Arrange given numbers to form the smallest number
- Minimum absolute difference between N and a power of 2
- Sort an array according to absolute difference with given value
- Find the node whose absolute difference with X gives minimum value
- Find the node whose absolute difference with X gives maximum value
- Pair with minimum absolute difference after solving each query
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- 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
- Longest subsequence such that adjacent elements have at least one common digit
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.



