🌱SproutStack
👤 Guest

🌱 DSA (Beginner) · Core Patterns · cozy lesson

Prefix Sum

10 min · 1 min read · no scary math, promise

🤖
You’ve got this. Read a little, play a little — I’ll wait. No rush.
pref = [0]
for x in a: pref.append(pref[-1]+x)
# sum l..r = pref[r+1]-pref[l]

def subarray_k(a, k):
    from collections import Counter
    c, cur, ans = Counter({0:1}), 0, 0
    for x in a:
        cur += x
        ans += c[cur-k]
        c[cur] += 1
    return ans

Check your understanding

Correct answers earn XP (once each).

1. Range sum l..r?

2. Subarray sum k counts via?

My notes (saved in this browser)

Select text above → Save selection, or write your own. AlgoMaster-style notebook, local-first for MVP.

No notes yet. Your highlights will live here.

Finished reading? Seal it with a tick ✅

The checkbox in the explorer turns green too — same progress.