Skip to content

Subarray Sum Equals K
Prefix Map Step by Step

Every subarray ending at j sums to k when an earlier prefix sum equals prefix[j] − k, so a map counts the prefixes.

Step back and forward through the map, then share a link to the step where a lookup failed.

What You Watch While the Map Fills

The array and the map change together on every step.

Every lookup answers one question, which is whether an earlier prefix left exactly k.

CodeStory's visualiser shows the array and the map filling up beside it, key by key. Highlighted code lines follow along, and you can step back, step forward or play.

Open Negatives first, where a sliding window fails but the map still works.

How the Prefix Map Counts Subarrays

Steps below follow the map on your screen.

  1. 1Start the map with a prefix sum of 0 seen once.
  2. 2Add the next number to the prefix sum so far.
  3. 3Look up the prefix sum minus k in the map and add its count to the answer.
  4. 4Store the new prefix sum in the map and add 1 to its count.
  5. 5Return the answer after the last number.

Where Subarray Sum Goes Wrong

Both switches live in the visualiser. Flip one on and watch the count break.

  • Brute force: every subarrayAdds up every subarray with a live total, so you get about n²/2 additions instead of n lookups.
  • Bug: map starts emptyForgets seen = {0: 1}, so every subarray that starts at index 0 goes missing. Load Only the whole array to see it.

Subarray Sum Inputs Worth Trying

Three presets cover negatives, zeros and the empty map bug.

  • NegativesA sliding window gets this wrong, because with negatives a bigger window can shrink the sum.
  • All zeros, k = 0Every subarray counts, so watch the counts in the map grow.
  • Only the whole arraySwitch on the empty map bug here, because the only answer starts at index 0.

Subarray Sum Cost in Time and Space

Interviewers ask why one pass beats checking every subarray, and the visualiser counts real operations.

Time
O(n)counted from the lookups and inserts you step through
Space
O(n)extra memory for the prefix map
Pattern
Arrays & hashingMemory buys back time.

Practise Hashing Beyond Subarray Sum

Watching shows the map and solving trains you to reach for it.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Subarray Sum Visualiser FAQs

Open the visualiser and follow the prefix map key by key.

Open the visualiser
What does the subarray sum equals k visualiser show?

CodeStory's visualiser shows the array and the prefix map filling up beside it, key by key, with the highlighted line of code. Pause, rewind or play whenever you like.

What is the time complexity of subarray sum?

O(n) time and O(n) space. Operation counts update live as you step, so you see the lookups add up instead of trusting a formula.

Why does a sliding window fail with negative numbers?

Negative numbers break it, because adding a number can shrink the sum. Load Negatives to see the window fail while the prefix map still counts correctly.

Why does my subarray sum code miss subarrays that start at index 0?

Your map needs a prefix sum of 0 seen once before the loop starts. Switch on the empty map bug in CodeStory's visualiser and load Only the whole array to watch the answer vanish.

Can I use my own input?

Yes. Enter your own array and a value for k, and the visualiser fills the prefix map for them.

Is the visualiser free?

Yes. New accounts get 100 AI credits and 100 more every month, and watching an algorithm spends none.

Stuck on Hashing? Get a Free Callback

Tell CodeStory which map step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from array basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings that suit your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.