Subarrays Summing to a Target
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
Given a list of integers, which may include negatives, and a target, count how many contiguous stretches of the list add up exactly to the target. Explain why a sliding window doesn't work here, and what does.
What a strong answer covers
- Why the usual window technique fails once negatives appear.
- How running totals let you express any stretch's sum.
- What to store as you go so each step is a quick lookup.
- The starting state that makes stretches from the very beginning count, and the overall cost.
Common mistake: Reaching for a sliding window because it works when every number is positive.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud