#K45572. Maximum Beauty Subarray
Maximum Beauty Subarray
Maximum Beauty Subarray
You are given an array of n distinct positive integers. A subarray is a contiguous part of the array. The beauty of a subarray is defined as the difference between its maximum and minimum elements, i.e.,
\(\text{beauty} = \max(S) - \min(S)\)
where \(S\) is a subarray with at least two elements. Your task is to compute the beauty of the most beautiful subarray. Note that if the array contains only one element, the beauty is defined to be 0.
Input: The first line contains a single integer n, denoting the number of elements in the array. The second line contains n space-separated integers, representing the array elements.
Output: Output a single integer, the beauty of the most beautiful subarray.
inputFormat
The input is read from standard input and contains two lines:
- The first line contains an integer
n
— the number of elements in the array. - The second line contains
n
space-separated integers representing the array.
outputFormat
Output a single integer to standard output — the beauty of the most beautiful subarray. If n = 1
, output 0
.
5
1 3 9 2 7
8