• + 0 comments

    Solved the question using Monotonic Stack in python:

    def solve(arr):
        left_greater = [0]*len(arr)
        right_greater = [0]*len(arr)
        stack = []
        for i in range(len(arr)):
            while stack and arr[stack[-1]]<=arr[i]:
                stack.pop()
            if stack:
                left_greater[i] = stack[-1]+1
            stack.append(i)
        stack = []
        for i in range(len(arr)-1,-1,-1):
            while stack and arr[stack[-1]]<=arr[i]:
                stack.pop()     
            if stack:
                right_greater[i] = stack[-1]+1
            stack.append(i)
        MaxIndexProduct = 0
        for i in range(len(arr)):
            MaxIndexProduct = max(MaxIndexProduct, left_greater[i]*right_greater[i])
        
        return MaxIndexProduct