Lexicographically smallest permutation of length 2 N that can be obtained from an N | Function to find the lexicographically smallest permutation of length 2 * N satisfying the given conditions ; Stores if i - th element is placed at odd position or not ; Traverse the array ; Mark arr [ i ] true ; Stores all the elements not placed at odd positions ; Iterate in the range [ 1 , 2 * N ] ; If w [ i ] is not marked ; Stores whether it is possible to obtain the required permutation or not ; Stores the permutation ; Traverse the array arr [ ] ; If it is S . end ( ) ; Mark found false ; Push arr [ i ] and * it into the array ; Erase the current element from the Set ; If found is not marked ; Otherwise , ; Print the permutation ; Given Input ; Function call
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 ( " " ) ; } 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 ) ;