CORRECT CODE INVESTIGATION / CASE FILE #002

Solve Two Sum

Determine when an unsorted pair search requires preserved evidence.

REASON BEFORE CODE.
01

THE ASSIGNMENT

Find Two Elements Whose Sum Equals the Target

The Objective

Given an unsorted array of integers named numbers and an integer target, identify two elements at distinct index positions whose values add up to the target.

The required result is the two index positions—not the values themselves.

The investigation must establish how a valid pair can be identified without assuming that the array is sorted or that the elements appear in any particular order.

REQUIRED OUTCOME Return two distinct indices identifying elements whose sum equals the target.
02

THE EVIDENCE

What Information Is Available?

The Input

The problem statement establishes the following evidence:

  • Array: An unsorted array of integers named numbers.
  • Target: An integer representing the required sum.
  • Indices: The positions identify where the selected elements are located.
  • Distinct positions: The same array position cannot be used twice.
  • Access: The array supports sequential examination and indexed access.
IMPORTANT EVIDENCE The array is unsorted. Its arrangement does not establish a directional relationship between values.

A value examined earlier may become important when a later value is evaluated.

03

THE CENTRAL DIFFICULTY

Finding One Candidate Is Not Enough

The Challenge

A solution must establish that two elements, at different positions, satisfy the required sum.

For each active candidate, the remaining amount needed to reach the target can be determined. However, calculating that amount does not establish whether a matching element exists.

The solution must also determine whether previously examined evidence can satisfy that requirement.

01

What Value Is Needed?

Determine the complementary value required to complete the current candidate and reach the target.

02

Has That Value Already Appeared?

Establish whether the complementary value has already been encountered at a different index.

03

How Can Earlier Evidence Be Preserved?

If the required value has not appeared, determine how the current candidate can remain available for a future comparison.

THE CENTRAL DIFFICULTY The difficulty is not simply examining the array. It is preserving the evidence needed to make a later decision.
04

THE CCF INVESTIGATION

Reason Through the Solution Before Choosing Its Implementation

REASON BEFORE CODE.

The Correct Code Framework organizes the investigation into distinct reasoning spaces and decision responsibilities.

Base Component

The Base Component establishes the identity of the solution: TwoSum.

Problem Space

Establish the input array, target, element positions, and the boundaries of the available evidence.

Answer Space

Define the required output: two distinct indices whose corresponding values sum to the target.

Decision Space

Determine which candidate to examine, what evidence is needed, and whether the current state supports a valid answer.

Persistence and Recorder

Determine whether evidence from previously examined candidates must remain available. Establish what must be preserved and how it supports later decisions.

Completion

Establish when a valid pair has been found and what result must be returned.

THE INVESTIGATION PRINCIPLE Derive each responsibility from the problem's requirements. Do not select an implementation because it is familiar or convenient.
05

THE INVESTIGATION STANDARD

Evidence Before Implementation

Scanner and Recorder

Two Sum introduces an important distinction: examining evidence and preserving evidence are separate responsibilities.

01

The Scanner

The Scanner determines which candidate is examined.

02

The Recorder

The Recorder preserves information that may be needed later.

The investigation must establish why both responsibilities are necessary, what information the Recorder must retain, and when that information becomes useful.

Only after the reasoning is established should the appropriate data structure, technique, algorithm, and implementation be selected.

THE OBJECTIVE The objective is not to memorize Two Sum. It is to prove why the solution works.

INVESTIGATION AUTHORIZED

Begin the Investigation

You have been introduced to the Two Sum problem, its available evidence, its central difficulty, and the reasoning methodology that will guide the investigation.

The investigation begins by establishing the Base Component and defining the required answer. From there, the evidence and decisions will determine what must be preserved and how it will be used.

The Scanner and Recorder must each fulfill a justified responsibility. Their relationship will be established through the evidence—not assumed before the investigation begins.

THE EVIDENCE COMES BEFORE THE CODE.
Begin Case File Investigation