Find the good permutation of first N natural numbers
Given an integer N, the task is to print a good permutation of first N natural numbers. Let’s denote the ith element of the permutation be pi.
A good permutation is a permutation such that for all 1 ≤ i ≤ N the following equations hold true,
- ppi = i
- pi != i
Basically above expressions mean, no value is equal to its position.
If no such good permutation exists then print -1.
Examples:
Input: N = 1
Output: -1
No good permutation exists.Input: N = 2
Output: 2 1
Position of 2 is 1 and position of 1 is 2.
Approach: Consider permutation p such that pi = i. Actually, p is a sequence of numbers from 1 to N and ppi = i.
Now the only trick is to change the permutation to satisfy the second equation i.e. pi != i. Let’s swap every two consecutive elements. More formally, for each k: 2k ≤ n let's swap p2k – 1 and p2k. It’s easy to see that the obtained permutation satisfies both the equations for every n with the only exception: when n is odd, there is no answer and we should print -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 good permutation // of first N natural numbers int printPermutation(int n) { // If n is odd if (n % 2 != 0) cout << -1; // Otherwise else for (int i = 1; i <= n / 2; i++) cout << 2 * i << " " << 2 * i - 1 << " "; } // Driver code int main() { int n = 4; printPermutation(n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the good permutation // of first N natural numbers static int printPermutation(int n) { // If n is odd if (n % 2 != 0) { System.out.println("-1"); } // Otherwise else for (int i = 1; i <= n / 2; i++) { System.out.print(2 * i + " " + ((2 * i) - 1) + " "); } return n; } // Driver code public static void main(String []args) { int n = 4; printPermutation(n); } } // This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function to print the good permutation # of first N natural numbers def printPermutation(n): # If n is odd if (n % 2 != 0): print(-1); # Otherwise else: for i in range(1, (n // 2) + 1): print((2 * i), (2 * i - 1), end = " "); # Driver code n = 4; printPermutation(n); # This code is contributed by mits |
C#
// C# implementation of the approach using System; class GFG { // Function to print the good permutation // of first N natural numbers static int printPermutation(int n) { // If n is odd if (n % 2 != 0) { Console.WriteLine("-1"); } // Otherwise else for (int i = 1; i <= n / 2; i++) { Console.WriteLine(2 * i + " " + ((2 * i) - 1) + " "); } return n; } // Driver code public static void Main(String []args) { int n = 4; printPermutation(n); } } // This code is contributed by Kirti_Mangal |
PHP
<?phP // PHP implementation of the approach // Function to print the good permutation // of first N natural numbers function printPermutation($n) { // If n is odd if ($n % 2 != 0) { echo("-1"); } // Otherwise else for ($i = 1; $i <= $n / 2; $i++) { echo(2 * $i . " " . ((2 * $i) - 1) . " "); } return $n; } // Driver code $n = 4; printPermutation($n); // This code contributed // by Code_Mech. ?> |
2 1 4 3
Recommended Posts:
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Find permutation of first N natural numbers that satisfies the given condition
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Program to find sum of first n natural numbers
- Find sum of N-th group of Natural Numbers
- Find if given number is sum of first n natural numbers
- Find m-th summation of first n natural numbers.
- Find the average of first N natural numbers
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Find the count of natural Hexadecimal numbers of size N
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Print all Good numbers in given range
- Find the number of good permutations
- Find all good indices in the given 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 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.



