#K77622. Maximum Difference in an Array
Maximum Difference in an Array
Maximum Difference in an Array
Given an array of integers, your task is to compute the maximum difference between any two elements such that the larger element appears after the smaller one in the array. Formally, you need to find:
$$\max_{0 \le i < j < n} (a_j - a_i)$$
If no such pair exists (for instance, when the array is non-increasing or if there is only one element), the output should be 0.
The solution should read input from standard input (stdin) and write the result to standard output (stdout).
inputFormat
The first line of input contains an integer n (where n ≥ 1), representing the number of elements in the array. The second line contains n space-separated integers, the elements of the array.
outputFormat
Output a single integer representing the maximum difference as described. If no valid pair is found, output 0.
## sample7
2 3 10 6 4 8 1
8
</p>