- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose there is a biker who is going on a road trip. There are n different points in his road trip at different altitudes. The biker starts his trip from point 0 with altitude 0. If we have a sequence called gain with n elements, gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). We have to find the highest altitude of a point.

So, if the input is like gain = [-4,2,6,1,-6], then the output will be 5, because the altitudes are [0,-4,-2,4,5,-1], so the maximum is 5.

To solve this, we will follow these steps −

maximum := 0

run_alt := 0

for each delta in gain, do

run_alt := run_alt + delta

maximum := max of maximum and run_alt

return maximum

Let us see the following implementation to get better understanding −

def solve(gain): maximum = 0 run_alt = 0 for delta in gain: run_alt += delta maximum = max(maximum, run_alt) return maximum gain = [-4,2,6,1,-6] print(solve(gain))

[-4,2,6,1,-6]

5

- Related Questions & Answers
- Python program to find the highest 3 values in a dictionary
- Find the highest 3 values in a dictionary in Python program
- Program to find the mid-point of a line in C++
- Find the altitude and area of an isosceles triangle in C++
- Program to find the final ranking of teams in order from highest to lowest rank in python
- Program to find the Break Even Point in C++
- How to find the highest number in a column?
- Program to find number of ways we can reach from top left point to bottom right point in Python
- MongoDB query to find the highest numeric value of a column?
- Program to find GCD of floating point numbers in C++
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- C++ program to find first collision point of two series
- Python Program to find the area of a circle
- Python Program to find the transpose of a matrix
- Python program to Find the size of a Tuple

Advertisements