Count subarrays for every array element in which they are the minimum | Set 2 | C # 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
using System ; using System . Collections . Generic ; class GFG { static void minSubarray ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int mini = int . MaxValue ; for ( int j = i ; j < N ; j ++ ) { mini = Math . Min ( mini , arr [ j ] ) ; if ( mp . ContainsKey ( mini ) ) { mp [ mini ] = mp [ mini ] + 1 ; } else { mp . Add ( mini , 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( mp [ arr [ i ] ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int N = arr . Length ; minSubarray ( arr , N ) ; } }