Digging the road downhill
A road crosses N segments with heights L[1..N]. The heights must become strictly decreasing from left to right, and they can only be lowered. On day d (d = 1, 2, 3, …) the team lowers every segment you choose that day by 2^(d − 1) metres. Find the minimum number of days needed.
Reported in: Infosys (SP / DSE · earlier papers) (memory-based)
Input
First line: N. Second line: N heights.
Output
The minimum number of days.
Constraints
1 ≤ N ≤ 10^5; heights fit in a 32-bit integer.
Example 1
Input:
3
3 3 3
Output:
2Example 2
Input:
4
9 5 7 1
Output:
2Example 3
Input:
3
5 4 1
Output:
0Hint: Lower each segment only as much as you must: going left to right, the new height is min(its height, the previous new height − 1). If the largest drop is R, the answer is the smallest T with 2^T − 1 ≥ R, because any drop up to 1 + 2 + … + 2^(T − 1) is a sum of different days.