CiscoOA #22
Find Maximum Difference
Estimated EasyJudge-ready
Topics (AI-labelled)Stock TradingIn-place Arrays
Among element pairs with i<j and a[j]>a[i], find the maximum of a[j]−a[i]; if there is no valid pair, output 0.
Input
The input is n followed by n integers. The original had no numeric bounds; this site adds 0≤n≤200000 and elements in −10^9..10^9; an empty array also outputs 0.
Output
Output a single integer, the answer.
Example 1
Input:
6
7 1 5 3 6 4
Output:
5
Example 2
Input:
3
9 7 4
Output:
0
Example 3
Input:
0
Output:
0
Example notes
The outputs of the three public examples are, in order: 5; 0; 0. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…