human
Count subarrays for every array element in which they are the minimum | Function to find the boundary of every element within which it is minimum.
Perform Binary Search.
Find mid m.
Update l.
Update r.
Inserting the index.
Function to required count subarrays.
Stores the indices of element.
Initialize the output array.
Left boundary , till the element is smallest.
Right boundary , till the element is smallest.
Calculate the number of subarrays based on its boundary.
Adding cnt to the ans.
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 binaryInsert ( boundary , i ) :
	l = 0
	r = len ( boundary ) - 1
	while l <= r :
		m = ( l + r ) // 2
		if boundary [ m ] < i :
			l = m + 1
		else :
			r = m - 1
	boundary . insert ( l , i )
	return l
def countingSubarray ( arr , n ) :
	index = { }
	for i in range ( n ) :
		index [ arr [ i ] ] = i
	boundary = [ - 1 , n ]
	arr . sort ( )
	ans = [ 0 for i in range ( n ) ]
	for num in arr :
		i = binaryInsert ( boundary , index [ num ] )
		l = boundary [ i ] - boundary [ i - 1 ] - 1
		r = boundary [ i + 1 ] - boundary [ i ] - 1
		cnt = l + r + l * r + 1
		ans [ index [ num ] ] += cnt
	return ans
N = 5
arr = [ 3 , 2 , 4 , 1 , 5 ]
print ( countingSubarray ( arr , N ) )