#K33552. Maximum Temperature Difference

    ID: 25112 Type: Default 1000ms 256MiB

Maximum Temperature Difference

Maximum Temperature Difference

You are given a list of integer temperature measurements. Your task is to calculate the maximum difference between any two measurements such that the cooler measurement comes before the warmer one. Formally, given a sequence of temperatures \(T_1, T_2, \dots, T_n\), find the maximum value of \(T_j - T_i\) where \(1 \leq i < j \leq n\). If no such pair exists (for example, if the list is in non-increasing order or contains less than two measurements), the answer should be 0.

Input Format

The first line contains an integer \(n\) representing the number of temperature measurements. The second line contains \(n\) space-separated integers representing the temperature measurements.

Output Format

Print a single integer denoting the maximum temperature difference found, or 0 if no valid difference exists.

inputFormat

The input consists of two lines:

  • The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)), representing the number of temperature readings.
  • The second line contains \(n\) space-separated integers. Each integer represents a temperature measurement. The temperatures can be negative or positive.

outputFormat

Output a single integer which is the maximum difference \(T_j - T_i\) with \(i < j\). If no such pair exists, output 0.

## sample
8
73 74 75 71 69 72 76 73
7