#C14364. Maximum Difference in an Array
Maximum Difference in an Array
Maximum Difference in an Array
You are given an array of integers. Your task is to find the maximum difference between any two elements such that the larger element comes after the smaller element. In other words, you need to compute
[ \max_{0 \leq i < j < n} { a[j] - a[i] } ]
if such a pair exists and the difference is positive, otherwise output 0.
Example: For the array [7, 1, 5, 3, 6, 4], the maximum difference is 5 (obtained from 6 - 1).
inputFormat
The input is given in the following format from stdin:
- The first line contains a single integer n (0 ≤ n ≤ 105), representing the number of elements in the array.
- The second line contains n space-separated integers, representing the elements of the array.
outputFormat
Output a single integer on stdout — the maximum difference between any two elements where the larger element appears after the smaller element. If no such pair exists, output 0.
## sample0
0
</p>