Question bank › Monotonic Stack
Dsa Monotonic Stack Medium

Elevation Gain Analysis

You are working on a hiking app that records the elevation gain over each segment of a hike. Given an array of elevation changes (which can be negative for a descent), you need to compute the total elevation gain observed after each segment. An elevation gain occurs only when the elevation is higher than the previous segment. If there is no gain, return 0. Write a program that reads an integer n (the number of elevation changes) followed by n integers representing the changes in elevation. Print a single line with n integers representing the elevation gain after each segment. Input format: - The first line contains a single integer n (1 <= n <= 100000). - The second line contains n space-separated integers representing the elevation changes. Output format: - A single line with n space-separated integers, the elevation gains. Example: Input: 5 2 -1 3 0 -2 Output: 2 0 3 0 0

Key concepts

monotonic_stackelevationgain

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.