DEV Community

Cover image for 1732. Find the Highest Altitude
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on

1732. Find the Highest Altitude


Image description

The problem statement says, we are given an array of altitudes of a rider. Now we have to sum them up and in the end check it with max value. If the sum value is larger than the max value then set the max value and return the value. But when adding the values we have to add the altitudes value with the previous value and the starting value is 0.


Image description


Image description


  1. take 2 variable, arr = 0 and max = 0;
  2. Run a for loop and set, arr += gain[i];
  3. Check if (max < arr), set max = arr;
  4. return max.


class Solution {
    public int largestAltitude(int[] gain) {
        int max = 0;
        int arr;
        arr= 0;
        for(int i = 0; i < gain.length; i++){
                arr += gain[i];
            if(max < arr){
                max = arr;
        return max;
Enter fullscreen mode Exit fullscreen mode


Image description

Top comments (0)