IBMOA #37
Get Minimum Increment
Estimated HardJudge-ready
Topics (AI-labelled)Binary SearchState Machines
Choose any set of pairwise non-adjacent positions and add the same non-negative integer x to all of them, in this single operation only, so that the array becomes non-decreasing. Find the minimum x; output -1 if it is impossible. If the array is already non-decreasing, you may choose the empty set and take 0.
Input
The first line contains n, and the second line contains the array. On this site 1≤n≤200000, −10^9≤element≤10^9.
Output
Output one integer, the answer.
Example 1
Input:
4
1 1 3 2
Output:
1
Example 2
Input:
3
3 2 1
Output:
-1
Example 3
Input:
3
1 2 3
Output:
0
Example notes
The examples give 1, -1 and 0 respectively.
Editorial
Loading solution…