Maximum Size Subarray Sum Equals k - Problem

You're given an integer array nums and a target integer k. Your mission is to find the maximum length of a contiguous subarray whose elements sum exactly to k.

A subarray is a contiguous sequence of elements within an array. For example, in array [1, 2, 3, 4], some subarrays include [1, 2], [2, 3, 4], and [1, 2, 3, 4].

Goal: Return the length of the longest subarray that sums to k. If no such subarray exists, return 0.

Example: In array [1, -1, 5, -2, 3] with k = 3, the subarray [5, -2] has sum 3 with length 2, but [1, -1, 5, -2, 3] also sums to 6... wait, that's not 3! The longest subarray summing to 3 is actually [3] with length 1, or we could have [1, -1, 5, -2] which sums to 3 with length 4!

Input & Output

example_1.py โ€” Basic case with positive result
$ Input: nums = [1, -1, 5, -2, 3], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: The subarray [1, -1, 5, -2] has sum = 1 + (-1) + 5 + (-2) = 3 and length = 4. This is the longest subarray with sum equal to 3.
example_2.py โ€” No valid subarray
$ Input: nums = [1, 2, 3], k = 10
โ€บ Output: 0
๐Ÿ’ก Note: No subarray sums to 10. The maximum possible sum of any subarray is 1+2+3=6, which is less than 10.
example_3.py โ€” Single element match
$ Input: nums = [1, 2, 1, 2, 1], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: Multiple subarrays sum to 2: [2] at index 1, [2] at index 3. Each has length 1, so the maximum length is 1.

Visualization

Tap to expand
๐Ÿฆ Bank Account Detective - Finding Period with Change = kDaily Changes: [+1, -1, +5, -2, +3] | Target Change: k = 3+1Day 1-1Day 2+5Day 3-2Day 4+3Day 5Running Balance (Prefix Sums):1After Day 10After Day 25After Day 33After Day 46After Day 5Detective's NotesBalance โ†’ First Seen Day0 โ†’ Before Day 11 โ†’ Day 15 โ†’ Day 33 โ†’ Day 46 โ†’ Day 5๐ŸŽฏ **Key Insight:** When we reach balance 3 on Day 4,we check: did we see balance (3-3=0) before? Yes, before Day 1!So Days 1-4 had net change of exactly 3. Length = 4 days!
Understanding the Visualization
1
Track Running Balance
Keep a cumulative sum (like your account balance after each day)
2
Record First Occurrences
Remember the first day you reached each balance amount
3
Look for Target Difference
When you see balance X, check if balance (X-k) was seen before
4
Calculate Period Length
The days between those two balances had a net change of exactly k
Key Takeaway
๐ŸŽฏ Key Insight: By tracking the first occurrence of each cumulative sum, we can instantly find the longest period with any target net change using the mathematical property: sum(i to j) = prefix_sum[j] - prefix_sum[i-1]

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array, with O(1) hash table operations

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash table can store up to n different prefix sums in worst case

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • -104 โ‰ค nums[i] โ‰ค 104
  • -107 โ‰ค k โ‰ค 107
  • The array can contain negative numbers, zero, and positive numbers
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 23
52.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen