Maximum size subarray sum equals k

December 7, 2022

array-and-hashmap

Problem URL: Maximum size subarray sum equals k

We will use prefix sum to solve this problem. We will store the prefix sum in a hash map. If the prefix sum is equal to k, then we will update the maximum length. If the prefix sum minus k is in the hash map, then we will update the maximum length. We will return the maximum length.

class Solution:
    def maxSubArrayLen(self, nums: List[int], k: int) -> int:
        prefixSum, res = 0, 0
        seen = {0: -1}
        for i, num in enumerate(nums):
            prefixSum += num
            if prefixSum == k:
                res = max(res, i+1)
            if prefixSum-k in seen:
                res = max(res, i-seen[prefixSum-k])
            if prefixSum not in seen:
                seen[prefixSum] = i
        return res

Time complexity: O(n)
Space complexity: O(n)