VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 03: Two pointers

Lesson 3.5 of 10 in this module Unrolled Pro

Two Sum II (Sorted Input)

Find the two values in a sorted array that add up to a target, using O(1) extra memory. From checking every pair, to a binary search per value, to a hash map, to converging pointers in O(n) time and O(1) space. Then every unique pair, counting pairs under a budget, the closest sum, and pairs across two arrays.

about 10 min 4 Python examples 2 quick checks

Problem. numbers is sorted in non-decreasing order. Return the 1-based indices [i, j] (i < j) of the two values that add up to target. Exactly one solution exists; use O(1) extra space.

text
numbers = [2, 7, 11, 15], target = 9    →   [1, 2]
numbers = [2, 3, 4],      target = 6    →   [1, 3]
numbers = [-1, 0],        target = -1   →   [1, 2]

ML context. "Two items whose costs fit a budget" is a common shape: two model calls whose latencies add up to an SLO, two features whose combined memory fits a device, two experiments whose sample sizes fill an allocation. With the costs sorted, two pointers answer it, and the counting variant (how many pairs fit) shows up in capacity planning.


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 "Two Sum II (Sorted Input)"

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