INTRODUCTION
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
HINTS
STEPS
- take 2 variable, arr = 0 and max = 0;
- Run a for loop and set, arr += gain[i];
- Check if (max < arr), set max = arr;
- 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;
}
}
Top comments (0)