Skip to content

ORIGINAL CODING QUESTION

Visible rooftop pairs

Count pairs i<j that can see each other: every intermediate height must be strictly below min(height[i],height[j]). Adjacent rooftops always qualify.

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; -100 ≤ value ≤ 100. Inputs contain exactly the stated values.

Example 1

Input

3 3 1 2

Output

3

All three pairs qualify.

Example 2

Input

3 1 3 2

Output

2

Only adjacent pairs qualify.

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 tall intermediate rooftop blocks short endpoints.

Hint 2

Handle equal heights carefully: they can themselves become blockers.

Hint 3

A decreasing monotonic structure tracks candidates visible to a new rooftop.

Practice this question

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

Open practice workspaceBrowse the question library →