human
Write me a code to count subarrays for every array element in which they are the minimum and the other steps required given the following:
Count subarrays for every array element in which they are the minimum | Function to required count subarrays.
For storing count of subarrays.
For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1..
For finding next smaller element right to a element if there is no next smaller element right to it than taking n ..
Taking exact boundaries in which arr [ i ] is minimum.
Similarly for right side.
Driver code.
Given array arr [ ].
Function call"
Solution in Python:
assistant
#Here is code to count subarrays for every array element in which they are the minimum and the other steps required.
 def countingSubarray ( arr , n ) :
	a = [ 0 for i in range ( n ) ]
	nsml = [ - 1 for i in range ( n ) ]
	nsmr = [ n for i in range ( n ) ]
	st = [ ]
	for i in range ( n - 1 , - 1 , - 1 ) :
		while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) :
			del st [ - 1 ]
		if ( len ( st ) > 0 ) :
			nsmr [ i ] = st [ - 1 ]
		else :
			nsmr [ i ] = n
		st . append ( i )
	while ( len ( st ) > 0 ) :
		del st [ - 1 ]
	for i in range ( n ) :
		while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) :
			del st [ - 1 ]
		if ( len ( st ) > 0 ) :
			nsml [ i ] = st [ - 1 ]
		else :
			nsml [ i ] = - 1
		st . append ( i )
	for i in range ( n ) :
		nsml [ i ] += 1
		nsmr [ i ] -= 1
		r = nsmr [ i ] - i + 1 ;
		l = i - nsml [ i ] + 1 ;
		a [ i ] = r * l ;
	return a ;
N = 5
arr = [ 3 , 2 , 4 , 1 , 5 ]
a = countingSubarray ( arr , N )
print ( a )