AmazonOA #262
Maximize Final Element
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
You may rearrange the array arbitrarily and decrease elements to integers of at least 1. The final first element must be 1, and a[i]−a[i−1]≤1 must hold for i≥1; find the maximum value of the last element. This is a one-sided difference condition, and the original array need not be sorted.
Input
The first line contains n; the second line contains the array. The original text had no numeric bounds; this site uses 1≤n≤200000, 1≤element≤10^9.
Output
Output a single integer, the answer.
Example 1
Input:
4
3 1 3 4
Output:
4
Example 2
Input:
1
100
Output:
1
Example 3
Input:
3
1 1 10
Output:
2
Example notes
The answers are 4, 1 and 2. A single element must become 1; you cannot simply take the maximum as the answer.
Editorial
Loading solution…