human
Longest Subarrays having each Array element as the maximum | Function to find the maximum length of Subarrays for each element of the array having it as the maximum.
Initialise the bounds.
Iterate to find greater element on the left.
If greater element is found.
Decrement left pointer.
If boundary is exceeded.
Iterate to find greater element on the right.
If greater element is found.
Increment right pointer.
if boundary is exceeded.
Length of longest subarray where arr [ i ] is the largest.
Print the answer.
Driver code"
How can the above be solved in Python?
 What is a code to longest Subarrays having each Array element as the maximum and the other steps required?
assistant
#Here is code to longest Subarrays having each Array element as the maximum and the other steps required.
 def solve ( n , arr ) :
	ans = 0
	for i in range ( n ) :
		left = max ( i - 1 , 0 )
		right = min ( n - 1 , i + 1 )
		while left >= 0 :
			if arr [ left ] > arr [ i ] :
				left += 1
				break
			left -= 1
		if left < 0 :
			left += 1
		while right < n :
			if arr [ right ] > arr [ i ] :
				right -= 1
				break
			right += 1
		if right >= n :
			right -= 1
		ans = 1 + right - left
		print ( ans , end = " ▁ " )
arr = [ 4 , 2 , 1 ]
n = len ( arr )
solve ( n , arr )