#K37537. Maximum Difference Between Array Elements
Maximum Difference Between Array Elements
Maximum Difference Between Array Elements
You are given an array of integers. Your task is to compute the maximum difference (\Delta = a_j - a_i) between any two elements such that the larger element (a_j) appears after the smaller element (a_i) in the array. If no such pair exists, return 0.
Formally, given an array (a) of length (n), find: [ \max_{0 \leq i < j < n} (a_j - a_i), ] with the condition that if (a_j \leq a_i) for every (j > i), then the answer is 0.
The problem simulates real-time analysis of data where you try to capture the largest positive change in a sequence.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer (n) representing the number of elements in the array. (n) can be zero.
- The second line (if (n > 0)) contains (n) space-separated integers representing the elements of the array.
outputFormat
Output a single integer to standard output (stdout) which is the maximum difference as defined by the problem. If the array is empty or no valid pair exists, output 0.## sample
6
7 1 5 3 6 4
5
</p>