Lexicographically smallest permutation of length 2N that can be obtained from an N-length array satisfying given conditions
Last Updated :
11 Oct, 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:
- 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.
- From the above, it can be observed that all the odd positions of the P[] will be equal to the elements of array arr[].
- 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].
- 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:
- Find the iterator pointing to the smallest integer greater than the integer, arr[i] using lower_bound(), and store it in a variable it.
- If it is equal to S.end() then print “-1” and return.
- Otherwise, Push the arr[i] and *it into the vector P and then erase the element pointed by iterator it.
- 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++
#include <bits/stdc++.h>
using namespace std;
void smallestPermutation( int arr[], int N)
{
vector< bool > w(2 * N + 1);
for ( int i = 0; i < N; i++) {
w[arr[i]] = true ;
}
set< int > S;
for ( int i = 1; i <= 2 * N; i++) {
if (!w[i])
S.insert(i);
}
bool found = true ;
vector< int > P;
for ( int i = 0; i < N; i++) {
auto it = S.lower_bound(arr[i]);
if (it == S.end()) {
found = false ;
break ;
}
P.push_back(arr[i]);
P.push_back(*it);
S.erase(it);
}
if (!found) {
cout << "-1\n" ;
}
else {
for ( int i = 0; i < 2 * N; i++)
cout << P[i] << " " ;
}
}
int main()
{
int arr[] = { 4, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
smallestPermutation(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static void smallestPermutation( int [] arr, int N)
{
boolean [] w = new boolean [ 2 * N + 1 ];
for ( int i = 0 ; i < N; i++) {
w[arr[i]] = true ;
}
Set<Integer> S = new HashSet<Integer>();
for ( int i = 1 ; i <= 2 * N; i++) {
if (!w[i])
S.add(i);
}
boolean found = true ;
Vector<Integer> P = new Vector<Integer>();
int [] p = { 4 , 5 , 1 , 2 , 3 , 6 };
for ( int i = 0 ; i < N; i++) {
if (S.contains(arr[i])) {
found = false ;
break ;
}
P.add(arr[i]);
P.add(arr[i]);
S.remove(arr[i]);
}
if (!found)
{
System.out.print( "-1" );
}
else
{
for ( int i = 0 ; i < 2 * N; i++)
System.out.print(p[i] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 4 , 1 , 3 };
int N = arr.length;
smallestPermutation(arr, N);
}
}
|
Python3
from bisect import bisect_left
def smallestPermutation(arr, N):
w = [ False for i in range ( 2 * N + 1 )]
for i in range (N):
w[arr[i]] = True
S = set ()
for i in range ( 1 , 2 * N + 1 , 1 ):
if (w[i] = = False ):
S.add(i)
found = True
P = []
S = list (S)
for i in range (N):
it = bisect_left(S, arr[i])
if (it = = - 1 ):
found = False
break
P.append(arr[i])
P.append(S[it])
S.remove(S[it])
if (found = = False ):
print ( "-1" )
else :
for i in range ( 2 * N):
print (P[i], end = " " )
if __name__ = = '__main__' :
arr = [ 4 , 1 , 3 ]
N = len (arr)
smallestPermutation(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void smallestPermutation( int [] arr, int N)
{
bool [] w = new bool [2 * N + 1];
for ( int i = 0; i < N; i++) {
w[arr[i]] = true ;
}
HashSet< int > S = new HashSet< int >();
for ( int i = 1; i <= 2 * N; i++) {
if (!w[i])
S.Add(i);
}
bool found = true ;
List< int > P = new List< int >();
int [] p = {4, 5, 1, 2, 3, 6};
for ( int i = 0; i < N; i++) {
if (S.Contains(arr[i])) {
found = false ;
break ;
}
P.Add(arr[i]);
P.Add(arr[i]);
S.Remove(arr[i]);
}
if (!found)
{
Console.WriteLine( "-1" );
}
else
{
for ( int i = 0; i < 2 * N; i++)
Console.Write(p[i] + " " );
}
}
static void Main()
{
int [] arr = { 4, 1, 3 };
int N = arr.Length;
smallestPermutation(arr, N);
}
}
|
Javascript
<script>
function smallestPermutation(arr, N)
{
let w = new Array(2 * N + 1);
for (let i = 0; i < N; i++) {
w[arr[i]] = true ;
}
let S = new Set();
for (let i = 1; i <= 2 * N; i++) {
if (!w[i])
S.add(i);
}
let found = true ;
let P = [];
let p = [4, 5, 1, 2, 3, 6];
for (let i = 0; i < N; i++) {
if (S.has(arr[i])) {
found = false ;
break ;
}
P.push(arr[i]);
P.push(arr[i]);
S. delete (arr[i]);
}
if (!found)
{
document.write( "-1" );
}
else
{
for (let i = 0; i < 2 * N; i++)
document.write(p[i] + " " );
}
}
let arr = [ 4, 1, 3 ];
let N = arr.length;
smallestPermutation(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...