Skip to content
Related Articles

Related Articles

Improve Article

Lexicographically smallest permutation of length 2N that can be obtained from an N-length array satisfying given conditions

  • Difficulty Level : Expert
  • Last Updated : 30 Jun, 2021

Given an array arr[] of size N, the task is to find the lexicographically smallest permutation of first 2*N natural numbers such that every ith element in the given array is equal to the minimum of the (2 * i)th and (2 * i – 1)th element of the permutation.

Examples:

Input: arr[] = {4, 1, 3}
Output: 4 5 1 2 3 6

Input: arr[] = {2, 3, 4, 5}
Output: -1

Approach: The given problem can be solved based on the following observations: 



  1. Assuming array P[] contains the required permutation, and from the condition that arr[i] = min(P[2*i], P[2*i-1]), then it is best to assign arr[i] to P[2*i-1] as it will give the lexicographically smaller permutation.
  2. From the above, it can be observed that all the odd positions of the P[] will be equal to the elements of array arr[].
  3. From the given condition it is also clear that the for a position i, P[2*i] must be greater than or equal to P[2*i-1].
  4. Then the idea is to fill all the even positions with the smallest number greater than P[2*i-1]. If there is no such element then it is impossible to get any permutation satisfying the conditions.

Follow the steps below to solve the problem:

  • Initialize a vector say W, and P to store if an element is in array arr[] or not, and to store the required permutation.
  • Initialize a set S to store all the elements in the range [1, 2*N] which are not in array arr[].
  • Traverse the array arr[] and mark the current element true in the vector W.
  • Iterate in the range [1, 2*N] using the variable i and then insert the i into set S.
  • Iterate in the range [0, N-1] using the variable i and perform the following steps:
  • Finally, after completing the above steps, if none of the above cases satisfy then print the vector P[] as the obtained permutation.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the lexicographically
// smallest permutation of length 2 * N
// satisfying the given conditions
void smallestPermutation(int arr[], int N)
{
 
    // Stores if i-th element is
    // placed at odd position or not
    vector<bool> w(2 * N + 1);
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Mark arr[i] true
        w[arr[i]] = true;
    }
 
    // Stores all the elements
    // not placed at odd positions
    set<int> S;
 
    // Iterate in the range [1, 2*N]
    for (int i = 1; i <= 2 * N; i++) {
 
        // If w[i] is not marked
        if (!w[i])
            S.insert(i);
    }
 
    // Stores whether it is possible
    // to obtain the required
    // permutation or not
    bool found = true;
 
    // Stores the permutation
    vector<int> P;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        // Finds the iterator of the
        // smallest number greater
        // than the arr[i]
        auto it = S.lower_bound(arr[i]);
 
        // If it is S.end()
        if (it == S.end()) {
 
            // Mark found false
            found = false;
            break;
        }
 
        // Push arr[i] and *it
        // into the array
        P.push_back(arr[i]);
        P.push_back(*it);
 
        // Erase the current
        // element from the Set
        S.erase(it);
    }
 
    // If found is not marked
    if (!found) {
        cout << "-1\n";
    }
    // Otherwise,
    else {
        // Print the permutation
        for (int i = 0; i < 2 * N; i++)
            cout << P[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { 4, 1, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    smallestPermutation(arr, N);
 
    return 0;
}

Python3




# Python3 program for the above approach
from bisect import bisect_left
 
# Function to find the lexicographically
# smallest permutation of length 2 * N
# satisfying the given conditions
def smallestPermutation(arr, N):
     
    # Stores if i-th element is
    # placed at odd position or not
    w = [False for i in range(2 * N + 1)]
 
    # Traverse the array
    for i in range(N):
         
        # Mark arr[i] true
        w[arr[i]] = True
 
    # Stores all the elements
    # not placed at odd positions
    S = set()
 
    # Iterate in the range [1, 2*N]
    for i in range(1, 2 * N + 1, 1):
         
        # If w[i] is not marked
        if (w[i] == False):
            S.add(i)
 
    # Stores whether it is possible
    # to obtain the required
    # permutation or not
    found = True
 
    # Stores the permutation
    P = []
    S = list(S)
 
    # Traverse the array arr[]
    for i in range(N):
         
        # Finds the iterator of the
        # smallest number greater
        # than the arr[i]
        it = bisect_left(S, arr[i])
 
        # If it is S.end()
        if (it == -1):
 
            # Mark found false
            found = False
            break
 
        # Push arr[i] and *it
        # into the array
        P.append(arr[i])
        P.append(S[it])
 
        # Erase the current
        # element from the Set
        S.remove(S[it])
 
    # If found is not marked
    if (found == False):
        print("-1")
         
    # Otherwise,
    else:
         
        # Print the permutation
        for i in range(2 * N):
            print(P[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    arr = [ 4, 1, 3 ]
    N = len(arr)
     
    # Function call
    smallestPermutation(arr, N)
     
# This code is contributed by SURENDRA_GANGWAR
Output: 
4 5 1 2 3 6

 

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :