Skip to content
Related Articles

Related Articles

Lexicographically smallest permutation where no element is in original position

View Discussion
Improve Article
Save Article
  • Last Updated : 05 Aug, 2022
View Discussion
Improve Article
Save Article

Given a permutation of first N positive integers, the task is to form the lexicographically smallest permutation such that the new permutation does not have any element which is at the same index as of the old one. If it is impossible to make such permutation then print -1.

Examples:

Input: N = 5, arr[] = {1, 2, 3, 4, 5}
Output: 2 1 4 5 3 
Explanation: It is the smallest lexicographically permutation possible 
following the condition for 0 to N – 1 such that arr[i] != b[i].

Input: N = 1, arr[] = {1}
Output: -1

 

Approach: To solve the problem follow the below idea:

  • First, create the lexicographically smallest permutation and check if arr[i] is the same as b[i]. If it is not the last element, then swap, b[i] and b[i + 1].
  • If it is the last element then swap b[i] and b[i – 1] because there is no element in front of b[i] as it is the last element.

Follow the below steps to solve the problem:

  • First, create a vector b of size N from 1 to N.
  • Run a loop on vector b from index 0 to N – 1.
    • If the elements are different then continue.
    • Else if i is not N – 1, swap b[i] and b[i + 1]
    • If i is not 0 but it is the last element, swap b[i] and b[i – 1]
    • Otherwise, if it is the only element, then print -1.
  • After executing the loop print the vector b.

Below is the implementation of the above approach:

C++




// C++ code to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
 
// Function to find lexicographically
// smallest permutation
void findPerm(int a[], int n)
{
 
    // Declare a vector of size n
    vector<ll> b(n);
 
    // Copy the vector a to b
    for (ll i = 0; i < n; i++) {
        b[i] = i + 1;
    }
 
    for (ll i = 0; i < n; i++) {
 
        // Elements are different
        if (a[i] != b[i])
            continue;
 
        // Elements are same
        if (i + 1 < n)
            swap(b[i], b[i + 1]);
        else if (i - 1 > 0)
            swap(b[i], b[i - 1]);
        else {
            cout << -1 << endl;
            return;
        }
    }
 
    // Print the lexicographically
    // smallest permutation
    for (ll i = 0; i < n; i++)
        cout << b[i] << " ";
 
    cout << endl;
}
 
// Driver Code
int main()
{
 
    int N = 5;
    int arr[] = { 1, 2, 3, 4, 5 };
 
    // Function call
    findPerm(arr, N);
    return 0;
}

Java




// Java code to implement the above approach
import java.io.*;
import java.util.*;
 
class GFG {
  // Function to find lexicographically
  // smallest permutation
  public static void findPerm(int a[], int n)
  {
 
    // Declare an array of size n
    int b[] = new int[n];
 
    // Copy the array a to b
    for (int i = 0; i < n; i++) {
      b[i] = i + 1;
    }
 
    for (int i = 0; i < n; i++) {
 
      // Elements are different
      if (a[i] != b[i])
        continue;
 
      // Elements are same
      if (i + 1 < n) {
        int temp = b[i];
        b[i] = b[i + 1];
        b[i + 1] = temp;
      }
      else if (i - 1 > 0) {
        int temp = b[i];
        b[i] = b[i - 1];
        b[i - 1] = temp;
      }
      else {
        System.out.println(-1);
        return;
      }
    }
 
    // Print the lexicographically
    // smallest permutation
    for (int i = 0; i < n; i++)
      System.out.print(b[i] + " ");
 
    System.out.println();
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 5;
    int arr[] = { 1, 2, 3, 4, 5 };
 
    // Function call
    findPerm(arr, N);
  }
}
 
// This code is contributed by Rohit Pradhan

Python3




# python3 code to implement the above approach
 
 
# Function to find lexicographically
# smallest permutation
def findPerm(a, n):
 
    # Declare a vector of size n
    b = [0 for _ in range(n)]
 
    # Copy the vector a to b
    for i in range(0, n):
        b[i] = i + 1
 
    for i in range(0, n):
 
        # Elements are different
        if (a[i] != b[i]):
            continue
 
        # Elements are same
        if (i + 1 < n):
            temp = b[i]
            b[i] = b[i+1]
            b[i+1] = temp
        elif (i - 1 > 0):
            temp = b[i]
            b[i] = b[i-1]
            b[i-1] = temp
        else:
            print(-1)
            return
 
    # Print the lexicographically
    # smallest permutation
    for i in range(0, n):
        print(b[i], end=" ")
 
    print()
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 5
    arr = [1, 2, 3, 4, 5]
 
    # Function call
    findPerm(arr, N)
 
    # This code is contributed by rakeshsahni

C#




// C# code to implement the above approach
using System;
 
class GFG
{
 
  // Driver Code
  public static void Main(string[] args)
  {
    int N = 5;
    int[] arr = { 1, 2, 3, 4, 5 };
 
    // Function call
    findPerm(arr, N);
  }
 
  // Function to find lexicographically
  // smallest permutation
  public static void findPerm(int[] a, int n)
  {
    // Declare an array of size n
    int[] b = new int[n];
 
    // Copy the array a to b
    for (int i = 0; i < n; i++) {
      b[i] = i + 1;
    }
 
    for (int i = 0; i < n; i++) {
      // Elements are different
      if (a[i] != b[i])
        continue;
 
      // Elements are same
      if (i + 1 < n) {
        int temp = b[i];
        b[i] = b[i + 1];
        b[i + 1] = temp;
      }
      else if (i - 1 > 0) {
        int temp = b[i];
        b[i] = b[i - 1];
        b[i - 1] = temp;
      }
      else {
        Console.WriteLine(-1);
        return;
      }
    }
 
    // Print the lexicographically
    // smallest permutation
    for (int i = 0; i < n; i++)
      Console.Write(b[i] + " ");
    Console.WriteLine();
  }
}
 
// This code is contributed by Tapesh(tapeshdua420)

Javascript




<script>
// Function to find lexicographically
// smallest permutation
function swat(a, b)
{
 
// create a temporary variable
a = a + b;
b = a - b;
a = a - b;
}
 
function findPerm(a, n)
{
 
    // Declare a array of size n
    let  b=new Array(n);
 
    // Copy the vector a to b
    for (let i = 0; i < n; i++) {
        b[i] = i + 1;
    }
 
    for (let i = 0; i < n; i++) {
 
        // Elements are different
        if (a[i] != b[i])
            continue;
 
        // Elements are same
        if (i + 1 < n)
            swat(b[i], b[i + 1]);
        else if (i - 1 > 0)
            swat(b[i], b[i - 1]);
        else {
            document.write(-1);
            document.write( "<br>");
            return;
        }
    }
 
    // Print the lexicographically
    // smallest permutation
    for (let i = 0; i < n; i++)
        document.write(b[i] + " ");
 
    document.write( "<br>");
}
 
// Driver Code
    let N = 5;
    let arr = [ 1, 2, 3, 4, 5 ];
 
    // Function call
    findPerm(arr, N);
     
    // This code is contributed by satwik4409.
   </script>

Output

2 1 4 5 3 

Time Complexity: O(N)
Auxiliary Space: O(N),


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!