Find the Highest Altitude

A biker goes on a road trip made of n + 1 points at different altitudes. The biker starts at point 0 with altitude 0. You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1. Return the highest altitude of any point.

30:00

Find the Highest Altitude
easy
Free written solution
Topics
Companies

A biker goes on a road trip made of n + 1 points at different altitudes. The biker starts at point 0 with altitude 0. You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1. Return the highest altitude of any point.

Example 1:
Input: {"gain":[-5,1,5,0,-7]}
Output: 1
Constraints:
  • n==gain.lengthn == \text{gain.length}

  • 1≤n≤1001 \leq n \leq 100

  • −100≤gain[i]≤100-100 \leq \text{gain}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"gain":[-5,1,5,0,-7]}

The trip starts at point 0, at altitude 0. That starting point already counts, so the highest altitude so far is 0.

altitude

0

highest

0

altitude profile (points 0..5)

01-6highest 0

gain (leg i goes from point i to i + 1)

-5

0

+1

1

+5

2

0

3

-7

4

Variables
VariableValue
altitude0
highest0
DepthFunction Call
Stack empty
0/11