Lexicographically smallest permutation with no digits at Original Index
Given an integer N. The task is to find the lexicographically smallest permutation of integer of the form: 12345…N such that no digit occurs at the index as in the original number, i.e. if P1P2P3…PN is our permutation then Pi must not be equal to i.
Note : N is greater than 1 and less than 10.
Examples :
Input : N = 5 Output : 21435 Input : N = 2 Output : 21
For smallest permutation, the smaller digits should be placed in starting. So, there are two cases to handle this problem.
- N is even, i.e. number of digits is even. In such case, if all odd digits got placed to next even index and all even digits got placed to their preceding indexes, we will have the smallest permutation satisfying the above condition.
- N is odd, i.e. the number of digits is odd. In this, all are similar to above case only change is that the last three digits are shuffled in a way such that their permutation is smallest. For example, if we have 123 as the last three digits then 231 is the smallest possible permutation.
Algorithm :
- If N is even:
- place all even digits (upto N) in increasing order at odd index.
- place all odd digits in increasing order at even index.
- else
- place all even digits (upto N-3) in increasing order at odd index.
- place all odd digits (upto N-4) in increasing order at even index.
- Place N at (N-1)th place, N-1 at (N-2)th and N-2 at Nth place.
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Lexicographically smallest permutation of a string with given subsequences
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Count Derangements (Permutation such that no element appears in its original position)
- Lexicographically next permutation in C++
- Lexicographically n-th permutation of a string
- How to find Lexicographically previous permutation?
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Find n-th lexicographically permutation of a string | Set 2
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Lexicographically smallest rotated sequence | Set 2
- Lexicographically smallest string of length N and sum K
- Construct lexicographically smallest palindrome
- Make a lexicographically smallest palindrome with minimal changes
Below is the implementation of the above approach:
C++
// C++ program to find the smallest permutaion #include <bits/stdc++.h> using namespace std; // Function to print the smallest permutation string smallestPermute(int n) { char res[n + 1]; // when n is even if (n % 2 == 0) { for (int i = 0; i < n; i++) { if (i % 2 == 0) res[i] = 48 + i + 2; else res[i] = 48 + i; } } // when n is odd else { for (int i = 0; i < n - 2; i++) { if (i % 2 == 0) res[i] = 48 + i + 2; else res[i] = 48 + i; } // handling last 3 digit res[n - 1] = 48 + n - 2; res[n - 2] = 48 + n; res[n - 3] = 48 + n - 1; } // add EOL and print result res[n] = '\0'; return res; } // Driver Code int main() { int n = 7; cout << smallestPermute(n); return 0; } |
Java
// Java program to find the smallest permutaion class GFG { // Function to print the smallest permutation static void smallestPermute(int n) { char res[] = new char[n + 1]; // when n is even if (n % 2 == 0) { for (int i = 0; i < n; i++) { if (i % 2 == 0) res[i] = (char)(48 + i + 2); else res[i] = (char)(48 + i); } } // when n is odd else { for (int i = 0; i < n - 2; i++) { if (i % 2 == 0) res[i] = (char)(48 + i + 2); else res[i] = (char)(48 + i); } // handling last 3 digit res[n - 1] = (char)(48 + n - 2); res[n - 2] = (char)(48 + n); res[n - 3] = (char)(48 + n - 1); } // add EOL and print result res[n] = '\0'; for (int i = 0; i < n ; i++) { System.out.print(res[i]); } } // Driver Code public static void main(String []args) { int n = 7; smallestPermute(n); } } // This code is contributed by ANKITRAI1 |
Python 3
# Python 3 program to find the # smallest permutaion # Function to print the smallest # permutation def smallestPermute( n): res = [""] * (n + 1) # when n is even if (n % 2 == 0) : for i in range(n): if (i % 2 == 0): res[i] = chr(48 + i + 2) else: res[i] = chr(48 + i) # when n is odd else : for i in range(n - 2 ): if (i % 2 == 0): res[i] = chr(48 + i + 2) else: res[i] = chr(48 + i) # handling last 3 digit res[n - 1] = chr(48 + n - 2) res[n - 2] = chr(48 + n) res[n - 3] = chr(48 + n - 1) # add EOL and print result res = ''.join(res) return res # Driver Code if __name__ == "__main__": n = 7 print(smallestPermute(n)) # This code is contributed by ita_c |
C#
// C# program to find the smallest // permutaion using System; class GFG { // Function to print the smallest // permutation static void smallestPermute(int n) { char[] res = new char[n + 1]; // when n is even if (n % 2 == 0) { for (int i = 0; i < n; i++) { if (i % 2 == 0) res[i] = (char)(48 + i + 2); else res[i] = (char)(48 + i); } } // when n is odd else { for (int i = 0; i < n - 2; i++) { if (i % 2 == 0) res[i] = (char)(48 + i + 2); else res[i] = (char)(48 + i); } // handling last 3 digit res[n - 1] = (char)(48 + n - 2); res[n - 2] = (char)(48 + n); res[n - 3] = (char)(48 + n - 1); } // add EOL and print result res[n] = '\0'; for (int i = 0; i < n ; i++) { Console.Write(res[i]); } } // Driver Code public static void Main() { int n = 7; smallestPermute(n); } } // This code is contributed // by Akanksha Rai |
PHP
<?php // PHP program to find the smallest permutaion // Function to print the smallest permutation function smallestPermute($n) { $res = array_fill(0, $n + 1, ""); // when n is even if ($n % 2 == 0) { for ($i = 0; $i < $n; $i++) { if ($i % 2 == 0) $res[$i] = chr(48 + $i + 2); else $res[$i] = chr(48 + $i); } } // when n is odd else { for ($i = 0; $i < $n - 2; $i++) { if ($i % 2 == 0) $res[$i] = chr(48 + $i + 2); else $res[$i] = chr(48 + $i); } // handling last 3 digit $res[$n - 1] = chr(48 + $n - 2); $res[$n - 2] = chr(48 + $n); $res[$n - 3] = chr(48 + $n - 1); } // add EOL and print result $res[$n] = '\0'; for ($i = 0; $i < $n ; $i++) { echo $res[$i]; } } // Driver Code $n = 7; smallestPermute($n); // This code is contributed by mits ?> |
2143675
Recommended Posts:
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.


