Smallest occurring element in each subsequence | Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call
function solve ( arr , N ) { var M = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { M . set ( i , arr [ i ] ) ; } arr . sort ( ( a , b ) => a - b ) ; var Count = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { Count . set ( arr [ i ] , parseInt ( Math . pow ( 2 , N - i - 1 ) ) ) ; } M . forEach ( ( value , key ) => { document . write ( Count . get ( value ) + ' ' ) ; } ) ; } var arr = [ 5 , 2 , 1 ] ; var N = arr . length ; solve ( arr , N ) ;