Maximum Size Subarray Sum Equals k

Updated on 11 June, 2025
Maximum Size Subarray Sum Equals k header image

Problem Statement

In this task, we are given an array of integers, nums, and another integer k. Our objective is to identify the longest contiguous subarray within nums that sums up exactly to k. The result should be the length of this subarray. If no such subarray exists that meets the sum condition, the function should return 0. This problem checks our ability to efficiently find subarray sums and handle potentially large arrays given the constraints.

Examples

Example 1

Input:

nums = [1,-1,5,-2,3], k = 3

Output:

4

Explanation:

The subarray [1, -1, 5, -2] sums to 3 and is the longest.

Example 2

Input:

nums = [-2,-1,2,1], k = 1

Output:

2

Explanation:

The subarray [-1, 2] sums to 1 and is the longest.

Constraints

  • 1 <= nums.length <= 2 * 105
  • -104 <= nums[i] <= 104
  • -109 <= k <= 109

Approach and Intuition

The immediate brute-force approach is to try calculating the sum for all possible subarrays and check for the condition where the sum equals k. However, due to the potential size of the array, this approach can be significantly inefficient, hence requiring a more sophisticated method.

  1. A more efficient method involves using a hashmap to reduce the time complexity. This technique revolves around the concept of prefix sum and the hashmap storage of these sums.
  2. As we iterate through the array, we can keep a running sum of the elements. For each position in the array, we calculate a current sum from the start up to that point.
  3. The core idea is that if at two different indices, say i and j where j > i, their prefix sums subtracted by k are equal, prefixSum[j] - prefixSum[i] = k; this implies the subarray [i+1, j] sums up to k.
  4. Using the hashmap, we can store each prefix sum encountered as a key, and its first occurrence as the value. During our iterations, for each sum currentSum, we check if (currentSum - k) exists in our map. If it exists, this means there is a previously seen prefix sum which, when subtracted from the current prefix sum, results in k.
  5. The difference between the current index and the index stored in our hashmap gives the length of the current subarray. We track the maximum length of such subarrays for our result.

To illustrate with the examples:

  • For nums = [1, -1, 5, -2, 3], k = 3, as we iterate over the array:
    • The hashmap becomes a tool to track occurrences of each prefix sum. When the sum of 3 is achieved at index 3, it checks against the hashmap to find that subtracting 0 (sum from index 0-0) results in the sum 3.
  • For nums = [-2, -1, 2, 1], k = 1, similarly by maintaining the running sum, when the sum equals 1, we find the longest segment that adheres to this sum.

Using this optimal approach not only respects the size constraint but also ensures that we efficiently determine the longest subarray summing to k by utilizing hashmap and prefix sums, thus reducing the time complexity significantly compared to the naive approach.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int longestSubArraySum(vector<int>& nums, int k) {
        long long sum = 0;
        long long maxLen = 0;
        unordered_map<long long, long long> sumIndices;
        for (int idx = 0; idx < nums.size(); idx++) {
            sum += nums[idx];

            // Check if the sum of all elements equals to k
            if (sum == k) {
                maxLen = idx + 1;
            }
            
            // Check if there is any subarray summing to k
            if (sumIndices.count(sum - k)) {
                maxLen = max(maxLen, (long long)idx - sumIndices[sum - k]);
            }

            // Store the index for this sum if not already stored
            if (!sumIndices.count(sum)) {
                sumIndices[sum] = idx;
            }
        }
        
        return maxLen;
    }
};

The C++ solution provided solves the problem of finding the maximum length of a subarray that sums to a given integer ( k ). This summary details the approach used to achieve this.

Here is a breakdown of the steps and logic implemented in the solution:

  1. Initialize sum to zero to store cumulative sums of the array and maxLen to zero to track the maximum length of the subarray.
  2. Use an unordered_map named sumIndices to track the first occurrence of each cumulative sum along with their corresponding indices.
  3. Iterate through the vector nums using an index variable idx. For each element, update sum by adding the current element.
  4. If the cumulative sum from start up to the current index equals k, update maxLen to idx + 1, which effectively accounts for the length of the array from the start to the current index.
  5. Check if sum - k exists in sumIndices. If it does, calculate the length from the index where the cumulative sum of sum - k was noted to the current index, and update maxLen if this subarray is longer than any previously found.
  6. Add the current sum to sumIndices with its index as a value, but only if this sum hasn't been seen before to ensure only the first occurrence is recorded.

The primary technique used is a hashmap to store sum indices, allowing constant time look-up and efficiently solving the problem by avoiding a naive nested loop approach. This solution uses additional memory for the hash map, but improves the time complexity significantly by processing each element in linear time. In choosing between efficiency and extra space, this implementation opts for faster processing times, crucial for handling larger data sets efficiently.

Implementing this logic correctly ensures that this solution works across a variety of inputs, effectively handling edge cases such as varying array sizes and sum values, including negative numbers. The time complexity is ( O(n) ) where ( n ) is the number of elements in nums, and the space complexity is also ( O(n) ) due to the additional data structure used for tracking indices of sums.

java
class Solution {
    public int findMaxLength(int[] nums, int target) {
        int sum = 0;
        int maxLength = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        
        for (int index = 0; index < nums.length; index++) {
            sum += nums[index];

            if (sum == target) {
                maxLength = index + 1;
            }

            if (map.containsKey(sum - target)) {
                maxLength = Math.max(maxLength, index - map.get(sum - target));
            }

            map.putIfAbsent(sum, index);
        }

        return maxLength;
    }
}

The provided Java solution tackles the problem of finding the maximum length of a contiguous subarray whose sum equals a specified integer ( k ) (designated as target in the parameters). Implement this by employing a hash map to track the cumulative sums of elements as you iterate through the array. Here’s a breakdown of how the solution works:

  • Initialize sum to 0 and maxLength to 0 to keep track of the running sum of the array elements and the maximum length of the subarray found so far, respectively.
  • Create a hash map (map) to store the first occurrence of each cumulative sum encountered and their corresponding indices in the array.
  • Use a loop to iterate through the nums array. For each element:
    • Update the sum by adding the current element.
    • Check if the sum equals the target. If yes, set maxLength to the current index plus one (since array indices are zero-based).
    • Check if the map contains a key sum - target. This suggests a previous subarray with a sum that, when added to target, achieves the current sum. If found, calculate and update maxLength as the difference between the current index and the index stored for sum - target.
    • Add the current sum to the map if it hasn’t been added before to ensure storing only the first index at which each cumulative sum occurs.

This efficient approach utilizes the key concept of cumulative sum and a hash map for fast look-up and update operations, achieving effective tracking and calculation of subarray lengths.

python
class Solution:
    def longestSubarrayWithSum(self, numbers: List[int], target: int) -> int:
        cumulative_sum = max_length = 0
        prefix_indices = {}
        
        for index, value in enumerate(numbers):
            cumulative_sum += value
            
            # Directly check if the sum from index 0 to current index equals target
            if cumulative_sum == target:
                max_length = index + 1
            
            # Check if there is a subarray that sums to target
            if (cumulative_sum - target) in prefix_indices:
                max_length = max(max_length, index - prefix_indices[cumulative_sum - target])
            
            # Store the first occurrence of each cumulative_sum
            if cumulative_sum not in prefix_indices:
                prefix_indices[cumulative_sum] = index
        
        return max_length

The problem "Maximum Size Subarray Sum Equals k" is effectively solved using a HashMap in the provided Python code, capturing the largest subarray length with a sum equal to the target. Below are the key elements and workings of the solution:

  • Initialize cumulative_sum to calculate ongoing sum and max_length to track the maximum subarray size.
  • Use a dictionary, prefix_indices, to store the first occurrence of each cumulative sum.
  • Iterate over the numbers in the array:
    • Update the cumulative_sum by adding the current array element.
    • Directly check if the current cumulative_sum equals the target, if so, update max_length to the current index plus one.
    • Check if the difference between the cumulative_sum and the target exists in prefix_indices. If it does, calculate the length of the current valid subarray, comparing and updating the max_length accordingly.
    • Capture the first occurrence of the cumulative_sum in the prefix_indices if not already existing.

This implementation ensures O(n) complexity, where n is the number of elements in the list, making it efficient for large datasets. By leveraging the prefix sum technique and the HashMap for instant lookup, the code efficiently finds the maximum subarray length for which the sum equals the given target.

Comments

No comments yet.