#C14865. Maximum Difference in an Array
Maximum Difference in an Array
Maximum Difference in an Array
Given an array of integers, find the maximum difference between any two elements such that the larger element appears after the smaller element in the array.
More formally, given an array (A = [a_1, a_2, \dots, a_n]), compute (\max_{1 \leq i < j \leq n}(a_j - a_i)). If the array has fewer than two elements or no such pair exists (i.e. the array is non-increasing), the result should be (0).
For example, for the array [2, 3, 10, 6, 4, 8, 1], the maximum difference is (10 - 2 = 8>.
inputFormat
Input is read from standard input (stdin). The first line contains a single integer (N) representing the number of elements in the array. If (N > 0), the second line contains (N) space-separated integers. In case (N = 0), there will be no second line.
outputFormat
Output to standard output (stdout) a single integer representing the maximum difference as defined above.## sample
7
2 3 10 6 4 8 1
8