Skip to content

ORIGINAL CODING QUESTION

Largest histogram cover

Each bar has width one. Return the largest rectangular area lying completely under consecutive histogram bars.

Open practice workspace

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

Input and output

n, then n integer values. Print one integer as described.

Constraints

1 ≤ n ≤ 200; 0 ≤ height ≤ 100.

Example 1

Input

6 2 1 5 6 2 3

Output

10

Bars 5 and 6 support area 10.

Example 2

Input

3 0 0 0

Output

0

No positive area exists.

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

A rectangle is limited by its shortest bar.

Hint 2

A monotonic stack can find each bar’s maximal span.

Hint 3

Flush pending bars at the end, often using a zero-height sentinel.

Practice this question

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

Open practice workspaceBrowse the question library →