DEV Community

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

Posted on

1732. Find the Highest Altitude

INTRODUCTION

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.

Examples

Image description

HINTS

Image description

STEPS

  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.

JavaCode

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

OUTPUT

Image description

Top comments (0)