Back to videos
Correct Code Video
Case Files Core Skill

Case File 003: Two Sum II

Watch constraints remove weak approaches and narrow the solution family.

Lesson Briefing

What this video teaches

This video walks through Case File 003: Two Sum II, a sorted-array investigation in the Correct Code Framework. The problem asks the learner to find two numbers in a 1-indexed array that is already sorted in ascending order, return their indices, and avoid using the same element twice.

The lesson shows how constraints narrow the lawful solution family. In Case File 002, Two Sum required a Recorder because the array was unsorted and prior evidence had to be remembered. In Case File 003, the sorted constraint changes the investigation. Because the values are ordered, the learner can use directional elimination instead of storing prior values. If the current sum is too small, the left pointer must move right to increase the sum. If the current sum is too large, the right pointer must move left to decrease the sum.

The central invariant is that the correct pair remains inside the active search window between the left and right pointers. Each pointer movement must eliminate only values that cannot participate in the answer. This transforms the problem from guesswork into a controlled reduction of the search space.

The walkthrough compares possible approaches such as nested loops, hash maps, and two pointers. Nested loops violate efficiency expectations, and a hash map ignores the sorted evidence. The two-pointer method satisfies all clues because it uses the array order, preserves constant memory, and reaches the answer in linear time.

The video then demonstrates execution with a target sum. The learner evaluates the current left and right values, compares their sum to the target, moves the correct pointer, and stops when the invariant confirms the matching pair. The lesson closes by emphasizing that the solution is validated before construction: code only transcribes the proven movement rules, stopping condition, and return contract.