Open In App

Lexicographically smallest permutation with no digits at Original Index

Last Updated : 09 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ program to find the smallest permutation
 
#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 permutation
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 permutation
 
# 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
// permutation
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 permutation
 
// 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
?>


Javascript




<script>
 
// Javascript program to find the smallest permutation
 
// Function to print the smallest permutation
function smallestPermute(n)
{
    var res = Array(n+1).fill(0);
 
    // when n is even
    if (n % 2 == 0) {
        for (var 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 (var 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;
    }
 
    for(var i =0; i<res.length; i++)
    {
      res[i] = String.fromCharCode(res[i]);
    }
 
    return res.join("");
}
 
// Driver Code
var n = 7;
document.write( smallestPermute(n));
 
</script>


Output

2143675

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(n)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads