Maximum Number of Non-Overlapping Subarrays With Sum Equals Target - Problem
Maximum Number of Non-Overlapping Subarrays With Sum Equals Target

You are given an array of integers nums and an integer target. Your goal is to find the maximum number of non-empty, non-overlapping subarrays such that each subarray has a sum equal to the target value.

Think of this as a greedy optimization problem: whenever you find a subarray that sums to the target, you should "claim" it immediately to maximize your total count. The key insight is that being greedy here actually leads to the optimal solution!

Example: If nums = [1,1,1,1,1] and target = 2, you can form 2 non-overlapping subarrays: [1,1] and [1,1], with one element left over.

Return the maximum number of such subarrays you can create.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,1,1,1,1], target = 2
โ€บ Output: 2
๐Ÿ’ก Note: We can form 2 non-overlapping subarrays: [1,1] (indices 0-1) and [1,1] (indices 2-3), both with sum 2. The remaining element [1] cannot form a subarray with sum 2.
example_2.py โ€” Mixed Values
$ Input: nums = [-1,1,1,1,-1,1], target = 0
โ€บ Output: 3
๐Ÿ’ก Note: We can form 3 non-overlapping subarrays: [-1,1] (sum=0), [1,-1] (sum=0), and the single element [0] if it existed. Actually: [-1,1] (indices 0-1), [1,1,-1] (indices 2-4), giving us 2 subarrays. Wait, let me recalculate: [-1,1] (indices 0-1, sum=0), [1,1,-1,1] won't work. Better: [-1,1] (0-1), [1,-1] (2,4) doesn't work due to gap. Optimal is: [-1,1] and [1,1,-1] for 2 total, but greedy gives [-1,1], then [1,1,-1], then remaining [1]. Actually optimal answer is 3: [-1,1], [1,1,-1], [1] won't work. The correct answer is 2: [-1,1] and [1,1,-1].
example_3.py โ€” No Valid Subarrays
$ Input: nums = [0,0,0], target = 1
โ€บ Output: 0
๐Ÿ’ก Note: No subarray can sum to 1 since all elements are 0, so we return 0.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -104 โ‰ค nums[i] โ‰ค 104
  • 0 โ‰ค target โ‰ค 106
  • Subarrays must be non-empty and non-overlapping

Visualization

Tap to expand
๐ŸŽฏ Greedy Treasure Hunter Strategy11111Target: 2 coins per treasure bag๐ŸŽ’ Treasure Bag 111Sum = 2 โœ“ SEALED!Positions 0-1๐ŸŽ’ Treasure Bag 211Sum = 2 โœ“ SEALED!Positions 2-31Leftover๐Ÿง  Hunter's Memory Trickโ€ข Remember all coin totals seen so far: {0, 1, 2, ...}โ€ข When current_total - target appears in memory โ†’ found treasure!โ€ข Immediately seal bag and clear memory to start freshโ€ข This greedy approach guarantees maximum bags! ๐Ÿ†Result: 2 treasure bags collected! ๐Ÿ’ฐ๐Ÿ’ฐ
Understanding the Visualization
1
Initialize Hunt
Start with empty treasure bag, tracking all coin counts seen (including 0 for empty bag)
2
Collect Coins
Add each coin to running total, checking if we can form target sum with previous position
3
Claim Treasure
When current_total - some_previous_total = target, immediately seal the treasure bag
4
Reset and Continue
Clear memory of previous totals and start fresh search from current position
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works optimally because taking a subarray as soon as we find it never prevents us from finding a better solution later. This is due to the non-overlapping constraint - earlier decisions don't affect future possibilities.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
89.2K Views
Medium-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