Day 2 โ€“ Max Average Subarray & Highest Altitude | Sliding Window + Prefix Sum | LeetCode Challenge

TanishiTanishi
2 min read

๐Ÿ—“๏ธ Date: July 26, 2025
๐Ÿ“Œ Challenge:
๐Ÿงฉ Problem 1: Maximum Average Subarray I โ€“ LeetCode #643 (Easy)
๐Ÿงฉ Problem 2: Find the Highest Altitude โ€“ LeetCode #1732 (Easy)
๐Ÿ’ป Topic: Sliding Window, Prefix Sum, Arrays


โœ… Problem Statement (Summary):

๐Ÿ”น Problem 1: Maximum Average Subarray I

Goal: Find the contiguous subarray of size k with the maximum average value.
Example:
Input: nums = [1,12,-5,-6,50,3], k = 4
โ†’ Output: 12.75000
Explanation: Subarray [12,-5,-6,50] โ†’ Sum = 51 โ†’ Avg = 12.75


๐Ÿง  What I Learned (Problem 1):

  • Sliding Window is efficient when we need to compute window sums repeatedly.

  • Reusing previous window's sum: subtract the first element, add the next.

  • Store the max window sum and divide at the end for result.


๐Ÿงช Code (C++):

class Solution {
public:
    double findMaxAverage(vector<int>& nums, int k) {
        int win_sum = 0, max_sum = 0;
        for (int i = 0; i < k; i++) {
            win_sum += nums[i];
        }
        max_sum = win_sum;
        for (int i = k; i < nums.size(); i++) {
            win_sum += (nums[i] - nums[i - k]);
            max_sum = max(win_sum, max_sum);
        }
        return double(max_sum) / k;
    }
};

๐Ÿ•’ Time Complexity: O(n)
๐Ÿ“ฆ Space Complexity: O(1)

๐Ÿ“ธ LeetCode Submission Screenshot:


โœ… Key Takeaways:

  • Use fixed-size sliding window for k-length subarrays

  • Keep track of running sum to optimize computation

  • Divide by k only once at the end


๐Ÿ”น Problem 2: Find the Highest Altitude

Goal: Return the highest point reached based on gain[] changes in altitude.
Example:
Input: gain = [-5,1,5,0,-7]
โ†’ Output: 1
Explanation: Altitudes = [0, -5, -4, 1, 1, -6], max = 1


๐Ÿง  What I Learned (Problem 2):

  • Treat gain array as prefix sum over time.

  • Start from altitude 0 and keep updating cumulative gain.

  • Track max altitude as we traverse.


๐Ÿงช Code (C++):

class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        int maxGain = 0;
        int netGain = gain[0];
        maxGain = max(netGain, maxGain);
        for (int i = 1; i < gain.size(); i++) {
            netGain += gain[i];
            maxGain = max(netGain, maxGain);
        }
        return maxGain;
    }
};

๐Ÿ•’ Time Complexity: O(n)
๐Ÿ“ฆ Space Complexity: O(1)

๐Ÿ“ธ LeetCode Submission Screenshot:


โœ… Key Takeaways:

  • Start altitude is always 0

  • Running prefix sum tracks cumulative altitude

  • Track max at each step to find peak


๐Ÿ“ˆ Progress Tracker:
โœ… Day: 2 / 30
๐Ÿงฉ Total Problems Solved: 3
๐Ÿงญ Focus Today: Sliding Window + Prefix Sum


๐Ÿ—‚๏ธ Tags:
#leetcode #30DaysChallenge #slidingwindow #prefixsum #cpp #dsa #arrays #maxaverage #altitude #codingjourney

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi