Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | Function to find lexicographically smallest permutation of [ 1 , A ] having B integers greater than all the previous elements ; Initial array ; Resultant array ; Append the first ( B - 1 ) elements to the resultant array ; Append the maximum from the initial array ; Copy the remaining elements ; Return the answer ; Driver Code
function getSmallestArray ( A , B ) { let arr = [ ] ; for ( let i = 0 ; i < A ; i ++ ) arr [ i ] = i + 1 ; let ans = [ ] ; for ( let i = 0 ; i < A ; i ++ ) ans . push ( 0 ) ; for ( let i = 0 ; i < B - 1 ; i ++ ) ans [ i ] = arr [ i ] ; ans [ B - 1 ] = arr [ A - 1 ] ; for ( let i = B ; i < A ; i ++ ) ans [ i ] = arr [ i - 1 ] ; return ans ; } let A = 3 ; let B = 2 ; let ans = getSmallestArray ( A , B ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] , " " ) ;