CodeLab / Problems / #1

1. Two Sum

Easy
ArrayHash MapTwo Pointers
Problem Statement
Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Assume exactly one solution exists, and you cannot use the same element twice.
Input: nums = [3, 2, 4, 6, 5], target = 10
Output: [2, 3]  // nums[2] + nums[3] = 4 + 6 = 10
High-Level Algorithm Steps
    Multi-Language Code & Execution Simulation
    Strategy:
    Compare Languages:
    Press play to start the simulation.
    Step 0 / 0
    Algorithm Performance & Complexity Chart
    SELECTED APPROACH
    Brute Force
    TIME COMPLEXITY
    O(n²)
    SPACE COMPLEXITY
    O(1)
    O(n²) — Quadratic (Brute Force) O(n log n) — Linearithmic (Sorting + Two Pointers) O(n) — Linear (Hash Map)
    Comprehensive Strategy Matrix
    Strategy Time Space Key Idea / Trade-offs
    Brute Force O(n²) O(1) Checks every pair. Simple, zero added memory, inefficient on large datasets.
    AI 1 — Hash Map O(n) O(n) Single pass. Stores seen values to instantly find complement in O(1) time.
    AI 2 — Two Pointers O(n log n) O(n) Sorts elements with original indices, then shrinks inward using two pointers.