#K72352. Maximum Difference in Non-overlapping Increasing Subarrays

    ID: 33734 Type: Default 1000ms 256MiB

Maximum Difference in Non-overlapping Increasing Subarrays

Maximum Difference in Non-overlapping Increasing Subarrays

You are given an array of integers \(nums\). A subarray is defined as a contiguous segment of the array, and an increasing subarray is one where every element is strictly greater than its previous element.

The task is to partition the array into non-overlapping increasing subarrays, then for each subarray, calculate the difference between its maximum and minimum elements. Your goal is to find and output the maximum such difference over all increasing subarrays.

If the array has less than two elements, or if no increasing subarray exists with more than one element, the answer is defined to be 0.

Formally, if a subarray \(A[i \ldots j]\) (with \(i < j\)) is strictly increasing (i.e. \(A[k] < A[k+1]\) for all \(i \le k < j\)), then define its difference as \(max(A[i \ldots j]) - min(A[i \ldots j])\). The answer is the maximum difference among all such increasing subarrays.

Note: The subarrays are considered non-overlapping in the sense that when an element breaks the increasing property, the current subarray ends and a new subarray begins.

inputFormat

The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer \(n\) (the number of elements in the array). The second line contains \(n\) space-separated integers representing the array \(nums\).

outputFormat

Print a single integer to standard output (stdout) representing the maximum difference as described above.

## sample
8
1 3 6 2 9 7 11 4
7