Enter your book access code

The curriculum, drills, tutor, and interviewer mode are for readers of Ace the Rust Interview. The code is printed in the Preface (Companion site access).

Don’t have the book yet? The landing page stays open — purchase unlocks this gym.

intermediate · order 71

Two-sum in a sorted slice

arr-two-sum-01Related book chapters: 15 (optional — this page is complete on its own)two-pointerarray

Slug: arr-two-sum-01 · Paired reading: Chapter 15

Prompt

fn two_sum_sorted(a: &[i32], target: i32) -> Option<(usize, usize)> — ascending slice; return indices i < j with a[i] + a[j] == target, or None. O(n) time, O(1) extra space. Indices are Copy — use that.