Isosceles Cube Triangle
InterviewTime limit1sMemory limit512 MB
Given column heights, find the largest h such that some 2h-1 consecutive columns can all be reduced to the shape 1,2,...,h,...,2,1.
- Level
Medium5 of 10
- Topics
- Array, Brute force, Prefix sum
- Solved
- No attempts yet
Problem
The brothers Sergio and Luiz built a wall out of small wooden cubes. They never finished it, so the columns have different heights.

Now they want to play a different game: take cubes away until a single complete isosceles triangle is left. A cube can only be taken from the top of a column, and a cube that has been taken away cannot be put back on another column.
An isosceles triangle of height occupies consecutive columns whose heights, from left to right, are exactly . The picture below shows the triangles of heights 1, 2, 3, 4 and 5.

Every column that is not part of the triangle has to be emptied completely. Given the heights of the columns of the wall in order, write a program that finds the greatest height a remaining triangle can have. In the wall of the first picture, which has 30 columns, the tallest possible triangle has height 7.
Input
The first line contains an integer , the number of columns of the wall. The second line contains integers , the heights of the columns from left to right.
Constraints
Output
Print a single line with one integer , the greatest height a remaining triangle can have.