#K47827. Maximum Difference Finder
Maximum Difference Finder
Maximum Difference Finder
You are given an array of integers. Your task is to find the maximum difference between any two elements such that the larger element appears after the smaller element.
Formally, if the array is represented as \(a_1, a_2, \dots, a_n\), you need to compute the maximum of \(a_j - a_i\) for all indices \(1 \le i < j \le n\). If no such pair exists (i.e. the array is non-increasing or contains only one element), output \(-1\).
Example: For the array [2, 3, 1, 7, 4, 6], the maximum difference is \(7 - 1 = 6\).
inputFormat
The first line contains a single integer \(n\) (the number of elements in the array).
The second line contains \(n\) space-separated integers \(a_1, a_2, \dots, a_n\).
outputFormat
Output a single integer representing the maximum difference \(a_j - a_i\) such that \(i < j\). If no such pair exists, print \(-1\).
## sample6
2 3 1 7 4 6
6