Before you solve the case
The missing partner investigation
Before you write code, prove why the current number needs evidence from an earlier frame.
Case File #002 exists because Two Sum introduces the first real Recorder problem. The active candidate cannot prove the answer alone; it needs a preserved complement.
Solve Two Sum teaches why professionals use a HashMap here: it records prior values so the current candidate can close the case in constant time.
How to Analyze Two Sum
The Correct Code Framework treats the problem statement as an evidence locker. Two Sum is not solved by pattern memory. It is solved by asking what evidence must be remembered.
1. Extract the contract: The answer must return two indices, not the values themselves.
2. Map the environment: The input is an unsorted integer array and a target sum.
3. Derive the invariant: The active candidate needs a complement where candidate + complement == target.
4. Prove the Recorder: Since the complement may have appeared earlier, prior values must be preserved as value → index.
Translate the contract into Recorder-backed movement
Formal Translation
Iterate through numbers from left to right. For each index i,
compute needed = target - numbers[i]. If needed already exists in the Recorder,
return [recorder[needed], i]. Otherwise record numbers[i] → i and continue.
CCF Translation
The Answer Space requires two indices. The Problem Space is an unsorted indexed array and a stable target. The Invariant is complement equality. The Recorder is a HashMap from value to index. The Scanner advances one index at a time while the Recorder preserves prior evidence.
Formal Translation
For each candidate, compute its complement and query a value-to-index map containing only earlier elements.
CCF Translation
The Scanner brings present evidence; the Recorder preserves prior evidence. Candidate Qualification occurs when the current suspect finds its required complement in an earlier Frame.
Elimination Map
| Route | Ruling | Why |
|---|---|---|
| Nested pair scan | Lawful but inefficient | It repeatedly reopens prior evidence and costs quadratic work. |
| Set only | Insufficient | It can preserve existence but loses the indices required by the Answer Space. |
| Sort and use boundaries | Risky here | Sorting changes original positions unless additional index evidence is preserved. |
| HashMap Recorder | Survives | It preserves exactly the prior value-to-index evidence the active candidate needs. |
Wrong Path Analysis
Brute force is correct, not unlawful. The professional upgrade is the HashMap Recorder because it stops reopening every earlier Frame and preserves the complement evidence needed by the current candidate.
Evidence Commitment
The current number does not close the case by itself. Commit the answer only when its complement is already present in the Recorder. That prior entry proves a different, earlier element exists. Lookup happens before recording so the current element cannot testify as its own partner.
Correctness Ruling
Every prior value is recorded with its index. For the active value, complement = target - current. If that complement exists, its stored index and the current index form a valid pair. Lookup-before-recording prevents index reuse; otherwise the current value is preserved for future candidates.
Common Mistakes
- Recording before lookup, which may let the active element match itself.
- Rejecting brute force as incorrect when it is lawful but inefficient.
- Returning values when the Answer Space requires indices.
- Confusing duplicate values with duplicate indices; values may match while positions remain distinct.
Transfer Question
Change the environment
What changes if the array is sorted? What must the Recorder or Answer Space preserve if the case asks for every qualifying pair instead of one?
Two Sum Simulator
Watch the Scanner inspect one candidate at a time. The Recorder keeps earlier values so the current candidate can ask whether its complement has already been seen.
Problem Statement Reference
Given an array of integers numbers and an integer target, return the indices of the two numbers such that they add up to target.
Each input has exactly one solution, and the same element may not be used twice.
Return the two indices once the matching pair is discovered.
✓ Case Closed
You solved Case File #002 by proving the Recorder.
You read the problem statement like evidence, identified that one candidate cannot close the case alone, and derived the HashMap Recorder before writing the implementation.
- Answer Space — return two indices.
- Problem Space — unsorted integer array
numbersand integertarget. - Invariant —
candidate + complement == target. - Operation — compute complement, lookup, then record.
- Recorder — HashMap value → index preserves prior evidence.
- Scanner — linear forward scan, one active candidate at a time.
- Finalizer — return
[recorder[complement], i]when the complement is found.
The Recorder followed the evidence. It was not chosen by preference.