Lexicographically smallest permutation with no digits at Original Index
Last Updated :
09 Sep, 2022
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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string smallestPermute( int n)
{
char res[n + 1];
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;
}
}
else {
for ( int i = 0; i < n - 2; i++) {
if (i % 2 == 0)
res[i] = 48 + i + 2;
else
res[i] = 48 + i;
}
res[n - 1] = 48 + n - 2;
res[n - 2] = 48 + n;
res[n - 3] = 48 + n - 1;
}
res[n] = '\0' ;
return res;
}
int main()
{
int n = 7;
cout << smallestPermute(n);
return 0;
}
|
Java
class GFG
{
static void smallestPermute( int n)
{
char res[] = new char [n + 1 ];
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);
}
}
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);
}
res[n - 1 ] = ( char )( 48 + n - 2 );
res[n - 2 ] = ( char )( 48 + n);
res[n - 3 ] = ( char )( 48 + n - 1 );
}
res[n] = '\0' ;
for ( int i = 0 ; i < n ; i++)
{
System.out.print(res[i]);
}
}
public static void main(String []args)
{
int n = 7 ;
smallestPermute(n);
}
}
|
Python 3
def smallestPermute( n):
res = [""] * (n + 1 )
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)
else :
for i in range (n - 2 ):
if (i % 2 = = 0 ):
res[i] = chr ( 48 + i + 2 )
else :
res[i] = chr ( 48 + i)
res[n - 1 ] = chr ( 48 + n - 2 )
res[n - 2 ] = chr ( 48 + n)
res[n - 3 ] = chr ( 48 + n - 1 )
res = ''.join(res)
return res
if __name__ = = "__main__" :
n = 7
print (smallestPermute(n))
|
C#
using System;
class GFG
{
static void smallestPermute( int n)
{
char [] res = new char [n + 1];
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);
}
}
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);
}
res[n - 1] = ( char )(48 + n - 2);
res[n - 2] = ( char )(48 + n);
res[n - 3] = ( char )(48 + n - 1);
}
res[n] = '\0' ;
for ( int i = 0; i < n ; i++)
{
Console.Write(res[i]);
}
}
public static void Main()
{
int n = 7;
smallestPermute(n);
}
}
|
PHP
<?php
function smallestPermute( $n )
{
$res = array_fill (0, $n + 1, "" );
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 );
}
}
else
{
for ( $i = 0; $i < $n - 2; $i ++)
{
if ( $i % 2 == 0)
$res [ $i ] = chr (48 + $i + 2);
else
$res [ $i ] = chr (48 + $i );
}
$res [ $n - 1] = chr (48 + $n - 2);
$res [ $n - 2] = chr (48 + $n );
$res [ $n - 3] = chr (48 + $n - 1);
}
$res [ $n ] = '\0' ;
for ( $i = 0; $i < $n ; $i ++)
{
echo $res [ $i ];
}
}
$n = 7;
smallestPermute( $n );
?>
|
Javascript
<script>
function smallestPermute(n)
{
var res = Array(n+1).fill(0);
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;
}
}
else {
for ( var i = 0; i < n - 2; i++) {
if (i % 2 == 0)
res[i] = 48 + i + 2;
else
res[i] = 48 + i;
}
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( "" );
}
var n = 7;
document.write( smallestPermute(n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...