PROBLEM 03 OF 75 · DEEP-DIVE PILOT

Two Sum

Given an array of integers and a target, return the indices of two different entries whose values add to the target. This lesson also returns an empty list when no pair exists. Return the earlier index first.

Know the exact contract

  • The same array position cannot be used twice. Equal values at different positions are allowed.
  • Inputs in this studio are finite integers. The published challenge may assume exactly one answer; this studio also tests the no-answer case.
EXAMPLE 1numbers = [2, 7, 11, 15], target = 9→ [0, 1]

2 + 7 = 9, and the positions are different.

EXAMPLE 2numbers = [3, 3], target = 6→ [0, 1]

The two threes have equal values but different indices.

EXAMPLE 3numbers = [1, 5, 9], target = 20→ []

No two positions produce the target.

From pair checks to a complement map

SIMPLE APPROACH

Compare every pair of different positions. This uses O(n²) time and O(1) extra space. It is easy to verify, but it repeats checks as the input grows.

IMPROVED APPROACH

Scan once from left to right. For a current value x, the only earlier value that can finish a pair is target − x. Look up that complement in a map before storing x. If it exists, return the saved index and the current index. Otherwise save x and continue.

Why the move is safe

Before examining index i, the map contains values from earlier indices only, each mapped to its earliest saved index. Therefore any map hit names a distinct prior position, and every pair ending at i can be found by exactly one complement lookup.

Cost

The map solution takes O(n) expected time with hash lookup and O(n) extra space. Worst-case hash behavior depends on the runtime. The pair comparison solution takes O(n²) time and O(1) extra space.

The tempting wrong move

For [3, 3] with target 6, storing the current 3 before looking up its complement can return [0, 0]. Checking first and inserting second prevents that invalid self-pair.

FOLLOW THE ACTUAL STATE

Which complement have we seen?

The saved values below are the map before the current lookup. Try equal values and a target of twice that value.

STEP 1 / 2

Index 0 has value 2. Need 7.

Saved earlier: nothing yet

No earlier 7 yet. Save 2 at index 0, then continue.
Result: [0,1]
GUIDED CHECK · BEFORE YOU CODE

Can you defend the map?

These choices help you inspect the reasoning. Your independent coding pass is measured separately.

1. What must the map contain before index i is checked?
2. For value x and target t, what do you look up?
3. For [3, 3] and target 6, what happens if you insert before lookup?
4. Why does a single pass find every valid pair?
5. What is the expected cost of the map approach?
INDEPENDENT SOLVE

Now write the code.

Write solve(data). data.numbers is the integer array and data.target is the target. Return [earlierIndex, laterIndex], or [] when no pair exists. The built-in tests use pairs with a unique valid answer.

Opening the code studio…

PROVE IT AGAIN TOMORROW

A fresh solve is the test.

Loading your saved attempts…

This lesson uses original explanations and examples. The external coding problem is available on LeetCode.