[EASIEST] [JAVA] [SOLUTION] [100%] for [BEGINNERS]
Approach\n1. Initialize Variables:\n    - Set sum to 0 (cumulative altitude).\n    - Set max to 0 (highest altitude).\n\n2. Iterate Through Gain Array:\n    - U
# Approach\n1. **Initialize Variables**:\n    - Set `sum` to 0 (cumulative altitude).\n    - Set `max` to 0 (highest altitude).\n\n2. **Iterate Through Gain Array**:\n    - Use a `for` loop to iterate through each element in the `gain` array.\n\n3. **Update Cumulative Sum**:\n    - Add the current `gain` value to `sum`.\n\n4. **Update Maximum Altitude**:\n    - If `sum` is greater than or equal to `max`, update `max` with `sum`.\n\n5. **Return Result**:\n    - Return the value of `max`.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int sum=0, max=0;\n        for(int i=0; i<gain.length; i++){\n            sum += gain[i];\n            max = sum>=max ? sum:max;\n        }\n        return max;\n    }\n}\n```\n\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/b6a42670-0446-4953-b0c7-39bbe9a508a0_1718520998.3608294.jpeg)\n
['Java']