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.

  1. 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.
  2. 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.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

2143675


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.