VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 02: Hashing

Lesson 2.8 of 12 in this module Unrolled Pro

Subarray Sum Equals K

Count the subarrays that sum to k, with negative numbers allowed: from O(n³) and O(n²) brute forces, to prefix sums plus a hash map of counts in one pass. Why a sliding window fails with negatives, and what changes for positive-only input, sums divisible by k and 2D grids.

about 10 min 6 Python examples 2 quick checks

Problem. Given integers nums (which may be negative) and a target k, return the number of contiguous subarrays whose sum equals k.

text
nums = [1, 1, 1], k = 2                       →   2
nums = [3, 4, 7, 2, -3, 1, 4, 2], k = 7       →   4

ML context. "How many windows of a signal sum to a target?" covers counting time windows where net change equals a threshold, budget windows in usage logs, or balanced stretches (relabel +1 / −1 and ask for sum 0, e.g. equal positive and negative predictions). It combines prefix sums (lesson 1.2) with hash maps (this module), and it is the pattern behind half the "subarray" problems you'll meet.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Subarray Sum Equals K"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee