38. Find the Highest Altitude

EasyArrayArray

A biker starts at altitude 0. Given an integer array gain of net altitude gains between consecutive points, return the highest altitude reached at any point during the trip (including the start at 0).

Input: An integer array gain of length n.

Output: Integer — highest altitude reached.

Examples

Example 1
Input: [-5,1,5,0,-7]
Output: 1
Explanation: Altitudes: 0,-5,-4,1,1,-6. Maximum = 1.
Example 2
Input: [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: Altitudes: 0,-4,-7,-9,-10,-6,-3,-1. Max=0 (start).
Example 3
Input: [1,2,3,4,5]
Output: 15
Explanation: Altitudes: 0,1,3,6,10,15. Max=15.

Constraints

Asked by

CognizantGoogleAmazonMetaBloombergMicrosoft
Solve this problem in the editor →