The Ordered Lineup
Solve Two Sum II with the Correct Code Framework
Sorted two-boundary Scanner
Directional evidence replaces persistent memory.
| Reasoning Move | Sorted Pair Search |
|---|---|
| Evidence Signal | Sorted collection and target sum |
| Route | Two-Boundary Scanner |
| Invariant | Boundary moves discard only impossible pairs |
| Recorder | Not required |
| Legal Answer | Two qualifying indices |
Core Principle: Sorted structure converts memory search into directional elimination.
Two Sum, but a different lawful investigation
Given an ascending sorted integer array numbers and a target, return the one-indexed positions of two values whose sum equals the target. Exactly one solution is guaranteed.
i and j such that numbers[i] + numbers[j] == target in a sorted array; the required output uses one-based positions.Choose the Scanner Before the Reveal
Which movement model becomes lawful because the collection is sorted?
- HashMap Recorder
- Two-Boundary Scanner
- Linear single-candidate Scanner
- Graph traversal
Which clue permits pair regions to be eliminated without recording prior values?
Key Clues
| Structural Clue | Structural Meaning | Component Controlled |
|---|---|---|
| Collection is sorted | Directional intelligence exists. | Scanner and Movement Law |
| Output requires two indices | Pair positions must survive the investigation. | Answer Space and Finalizer |
| Target is a sum constraint | The Invariant evaluates a pair, not one candidate. | Frame and Candidate Qualification |
| Sum is too small or too large | One boundary can be repaired lawfully. | Decision Space |
| Boundaries cross | No active pair remains. | Scope Exhaustion |
Answer Space
| Terminal Evidence Structure | Two-index collection |
|---|---|
| Evidence Type | One-indexed positions |
| Evidence Commitment | Commit the active boundary pair when its sum matches |
| Finalizer | Return [left + 1, right + 1] |
Problem Space
| Environment Type | Ascending sorted integer array |
|---|---|
| Evaluation Material | Left value, right value, sum, and target |
| Active Frame | (numbers[left], numbers[right]) |
| Searchable Scope | The active pair region between both boundaries |
Decision Space
sum = numbers[left] + numbers[right]
sum == target → commit indices
sum < target → move left rightward
sum > target → move right leftward
Recorder or No Recorder?
Case File #002 required persistent memory. Does this case?
- HashMap Recorder required
- Set Recorder required
- No Recorder required
- DP table required
Which clue lets structure replace memory?
Invariant
Invariant Rule: All pair regions outside the active boundary frame have been proven unable to form the target; if a valid pair exists, it remains between left and right.
Scanner Selection
Derived Scanner: Two-Boundary Scanner.
It survives because the input is sorted, the target evaluates a pair Frame, sum comparison supplies directional repair, and impossible regions can be discarded safely.
Expert Think Aloud: I am not choosing two pointers because the name is familiar. I am choosing two boundaries because sorted order proves exactly which boundary may move after each failed sum.
Evidence Locker Decision: Not Opened
In Case File #002, the current suspect needed a preserved complement from an earlier Frame. Here, the sorted lineup already gives the detective directional evidence. If the sum is too small, the left boundary must move right. If the sum is too large, the right boundary must move left.
No HashMap is required because the structure of the evidence itself tells us which region can be eliminated.
Candidate Qualification
- Both values must remain inside the active pair region.
leftandrightmust identify different elements.- The pair must satisfy
numbers[left] + numbers[right] == target. - The Finalizer must honor the witness statement’s indexing requirement.
Evidence Commitment
The answer is not committed because one value looks promising. Commitment occurs only when the active pair proves the Invariant. Boundary movement is not guessing; it is evidence-based elimination that preserves every still-lawful candidate.
Boundary Repair
The current pair sum is too small. Which boundary must move?
- Move left rightward
- Move right leftward
- Move both boundaries
- Store the pair in a Recorder
Which movement makes a larger sum possible without violating the Invariant?
| Clue | What It Eliminates | What Survives | Why |
|---|---|---|---|
| Sorted array | HashMap as required memory, random movement | Two-Boundary Scanner | Order supplies directional movement intelligence. |
| Pair-sum target | Single-candidate Frame | Pair Frame | One value cannot qualify alone. |
| Sum too small | Move right leftward | Move left rightward | A larger sum requires a larger lower value. |
| Sum too large | Move left rightward | Move right leftward | A smaller sum requires a smaller upper value. |
| Boundary crossing | Infinite traversal | Scope Exhaustion | No pair remains in the active frame. |
Why Tempting Routes Fail
HashMap Recorder
Why it seems reasonable: Case File #002 used one for Two Sum.
Why it weakens the investigation: Sorted input already gives the Scanner directional intelligence, so persistent memory is unnecessary.
CCF Diagnosis: The Recorder is allowed but not required; using it ignores structural evidence.
Correct Move: Use a Two-Boundary Scanner.
Single Linear Scanner
Why it seems reasonable: Case File #001 evaluated one active candidate.
Why it fails: The Invariant is a pair-sum relationship, so a single candidate cannot qualify alone.
CCF Diagnosis: The Frame size is wrong.
Correct Move: Evaluate two boundary candidates as one pair Frame.
Binary Search Every Complement
Why it seems reasonable: Sorted input makes binary search legal.
Why it weakens the investigation: It repeatedly searches for complements instead of using the stronger boundary movement law.
CCF Diagnosis: It is legal, but not the cleanest structural survivor.
Correct Move: Use one two-boundary pass.
Follow the Boundary Frames
Use numbers = [2, 7, 11, 15] and target = 9.
| Frame | Left | Right | Sum | Decision | Action |
|---|---|---|---|---|---|
| 1 | 0 → 2 | 3 → 15 | 17 | Too large | Move right left. |
| 2 | 0 → 2 | 2 → 11 | 13 | Too large | Move right left. |
| 3 | 0 → 2 | 1 → 7 | 9 | Qualifies | Commit indices. |
[1, 2].Run the active pair investigation
Advance one Frame at a time. Watch the comparison identify which boundary may move without violating the Invariant.
Why every boundary move is lawful
- Active pair:
leftandrightrepresent the current candidate. - Sum too small: every pair using the current left value with this or any smaller right value cannot reach the target.
- Sum too large: every pair using the current right value with this or any larger left value exceeds the target.
- Maintenance: each move removes only impossible candidates, so any valid pair remains inside the boundaries.
- Termination: equality closes the case; crossed boundaries prove Scope Exhaustion.
- Using a HashMap when sorted directional evidence is already sufficient.
- Moving both pointers after every comparison.
- Forgetting the one-based index contract.
- Treating sorted Two Sum II as the same Problem Space as unsorted Two Sum.
- Moving a boundary without proving why the discarded region is impossible.
Transfer Question
Remove the strongest clue
What changes if the sorted guarantee disappears? What new evidence would make a Recorder lawful again?
Prove You Can Rebuild the Route
- What clue makes boundary repair lawful?
- Why is a Recorder unnecessary here but required in Case File #002?
- Which boundary moves when the sum is too small?
- What does the Invariant say about discarded regions?
- Why must the Finalizer adjust the indices?
Repair the Next Frame
With numbers = [1, 3, 5, 8], target = 9, and active boundaries left = 0, right = 3, complete the reasoning.
- Current sum: ________
- Boundary repair: ________
- Why that repair preserves the Invariant: ________
✓ Case Closed
You derived the route before writing code.
Sorted order supplied directional evidence, the pair-region Invariant protected the answer, and the Finalizer committed one-indexed positions.
- Answer Space
- Problem Space
- Invariant
- Operation
- Recorder Decision
- Scanner
- Finalizer