AmazonOA #162
Find Maximum Number of Balanced Combinations
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & Heaps
Following the source's enumeration, a combination means a non-empty contiguous subarray. A combination is balanced if its last item is not the maximum of the subarray, that is, some earlier number is strictly greater. Find the total number of balanced combinations; combinations may overlap.
Input
The first line contains n, and the second line contains n integers. This site sets 1≤n≤200000 and elements in −10^9..10^9.
Output
Output a single integer answer.
Example 1
Input:
3
3 6 3
Output:
2
Example 2
Input:
2
3 3
Output:
0
Example 3
Input:
3
5 4 3
Output:
3
Example notes
Example 1: [3,6,3] and [6,3] are balanced, 2 in total. Example 2: the last item of every segment is tied for the maximum and no element is strictly greater, so the answer is 0. Example 3: all three contiguous segments of length ≥2 are balanced, so the answer is 3.
Editorial
Loading solution…