Skip to content

ORIGINAL CODING QUESTION

Widest storage bay

Partitions stand at positions 0 to n-1 with given nonnegative heights. Choosing two partitions i < j forms a bay whose capacity is (j - i) multiplied by the shorter of the two heights; partitions between them are ignored. Return the largest capacity.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

First integer n, then n integers. Print one integer.

Constraints

2 ≤ n ≤ 200; 0 ≤ height ≤ 1000.

Example 1

Input

5
3 1 4 2 5

Output

12

Partitions 0 and 4 are 4 apart and the shorter is 3, giving 12.

Example 2

Input

2
0 9

Output

0

A zero-height partition gives capacity 0.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Start with the widest possible bay and think about which side to give up.

Hint 2

Moving the taller side inward can never help, because the shorter side still limits the height while the width shrinks.

Hint 3

Keep pointers at both ends, record the capacity, then move the pointer at the shorter partition inward until they meet.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →