#K13916. Maximum Height Difference

    ID: 24019 Type: Default 1000ms 256MiB

Maximum Height Difference

Maximum Height Difference

You are given an array of n integers representing the heights of trees in a row. Your task is to find the maximum height difference \(h_j - h_i\) where \(i < j\) and \(h_i < h_j\). In other words, among all pairs of trees where the tree on the left is shorter than the tree on the right, compute the maximum difference in their heights. If no such pair exists, output 0.

Note: The difference is computed as \(h_j - h_i\) where \(0 \le i < j \le n-1\). You are required to use an efficient approach with a single pass through the array.

inputFormat

The input consists of two lines:

  • The first line contains a single integer n indicating the number of trees.
  • The second line contains n space-separated integers representing the heights of the trees.

Constraints: \(1 \le n \le 10^5\); the tree heights are integers.

outputFormat

Output a single integer representing the maximum height difference as defined above. If no valid pair exists, output 0.

## sample
5
1 2 6 4 8
7

</p>