First we will create a stack of all buildings which have consecutively increasing heights. Then we will calculate the area by each min height by multiplying the number of buildings i.e. the index at which that building is and the buildings which are after it. Then we can calculate the maximum area which is formed. CODE: def computeLargestArea(hist, N): stack = [] max_area = 0 idx = 0 while (idx < N): if not stack or hist[stack[-1]] <= hist[idx]: stack.append(idx) idx += 1 else: height_idx = stack.pop() depth = idx if stack: depth = idx - stack[-1] - 1 area = hist[height_idx] * depth max_area = max(area, max_area) while stack: height_idx = stack.pop() depth = idx if stack: depth = idx - stack[-1] - 1 area = hist[height_idx] * depth max_area = max(area, max_area) return max_area def main(): N = int(raw_input()) hist = map(int, raw_input().split()) print computeLargestArea(hist, N) if __name__ == '__main__': main()