Count subarrays for every array element in which they are the minimum | Set 2 | Java program for the above approach ; 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
import java . util . * ; class GFG { static void minSubarray ( int [ ] arr , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int mini = Integer . MAX_VALUE ; for ( int j = i ; j < N ; j ++ ) { mini = Math . min ( mini , arr [ j ] ) ; if ( mp . containsKey ( mini ) ) { mp . put ( mini , mp . get ( mini ) + 1 ) ; } else { mp . put ( mini , 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( mp . get ( arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int N = arr . length ; minSubarray ( arr , N ) ; } }