Count subarrays for every array element in which they are the minimum | Set 2 | Function to calculate total number of sub - arrays for each element where that element is occurring as the minimum element ; Map for storing the number of sub - arrays for each element ; Traverse over all possible subarrays ; Minimum in each subarray ; Print the result ; Driver Code ; Function Call
function minSubarray ( arr , N ) { var m = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { var mini = 1000000000 ; for ( var j = i ; j < N ; j ++ ) { mini = Math . min ( mini , arr [ j ] ) ; if ( m . has ( mini ) ) m . set ( mini , m . get ( mini ) + 1 ) else m . set ( mini , 1 ) } } for ( var i = 0 ; i < N ; i ++ ) { document . write ( m . get ( arr [ i ] ) + " " ) ; } } var arr = [ 3 , 2 , 1 , 4 ] ; var N = arr . length ; minSubarray ( arr , N ) ;