Problem LeetCode 1. Two Sum ↗ · NeetCode ↗· The solutions on this page are my own.

Two Sum Visualizer

Four solutions run step by step next to their Java code, from the slowest to the fastest. Read the problem first, then pick an input or type your own and every solution reruns on it.

The problem, step by step

Skip to the visualizer ↓

Find the two numbers that add up to a target

You get a list of whole numbers and one target number. Exactly two of the numbers add up to the target. Your job is to say where they are.

  1. What you get

    An array nums with at least two integers, and an integer target. The numbers can be negative, zero or repeated, and they are in no particular order.

  2. What you return

    The two positions [i, j] where nums[i] + nums[j] == target. Positions (indexes), not the values. Either order of the two is accepted.

  3. The rules

    • There is always exactly one answer, so you never have to report "not found".
    • You may not use the same position twice. Two equal values at two different positions are fine.
    • The answer refers to the original positions, so if you reorder the numbers you must remember where each one came from.
  4. Try it by hand

    Input
    nums = [3, 8, 5, 1, 10], target = 9
    Output
    [1, 3]
    value on top, index below
    1. Take position 0, which holds 3. Its partner would have to be 9 − 3 = 6. There is no 6 in the array, so 3 is not part of the answer.
    2. Take position 1, which holds 8. Its partner would have to be 9 − 8 = 1.
    3. Look for a 1. It is at position 3, which is a different position from 1, so it is allowed.
    4. The answer is [1, 3], because 8 + 1 = 9.
  5. Edge cases to check

    • [3, 3] with target 6 gives [0, 1]: the same value at two positions.
    • [3, 2, 4] with target 6 gives [1, 2]. Using the 3 twice would also make 6, but that is one position used twice.
    • [-4, 7, 2] with target -2 gives [0, 2]. Negative numbers follow the same rule.
    • With only two numbers, the answer is always [0, 1].
  6. The question behind the solutions

    Checking every pair always works, but n numbers make about n² ÷ 2 pairs: 50 million checks for 10,000 numbers. For each number you already know the partner you need. How do you find out whether that partner exists without scanning the whole array again?

Four ways to find the pair

The example from above, nums = [3, 8, 5, 1, 10] and target 9, sketched the way you would draw it in an interview. Each board shows what the approach keeps in memory and the steps it runs. Then step through all four below.

1. Brute force TwoSumBruteForce

Remembers: nothing but the two loop positions.

38511001234numsi = 0: 3 + 8, 3 + 5, 3 + 1, 3 + 10, never 98 + 5 = 138 + 1 = 9 ✓6 pairs checked before the answer [1, 3]
  1. For each position i, try every later position j.
  2. If nums[i] + nums[j] is the target, return [i, j].
  3. Starting j after i means no pair is tried twice and no position is used twice.

The catch: n numbers make about n² ÷ 2 pairs. Here it took 6 tries; 10,000 numbers can take 50 million.

2. Sort + two pointers TwoSumSortTwoPointers

Remembers: a sorted copy of (value, original index) pairs.

sortedwas at135810#3#0#2#1#4LR1 + 10 = 11, too big: move R left135810#3#0#2#1#4LR1 + 8 = 9 ✓ they were at #3 and #1return [1, 3]
  1. Pair each value with its index and sort the pairs by value.
  2. Put L on the smallest value and R on the largest.
  3. Sum too big: move R left. Too small: move L right. Equal: return the two original indexes.

The catch: sorting costs O(n log n), and it loses the positions unless you carry them along, as the "was at" row does.

3. Hash map, two passes TwoSumHashMapTwoPass

Remembers: a map from every value to its index.

38511001234pass 1: store allvalueindex30815213104pass 2: look up the partner3 needs 9 − 3 = 6: not there8 needs 9 − 8 = 1: index 3 ✓return [1, 3]
  1. First pass: store value → index for every number.
  2. Second pass: for each i, work out need = target − nums[i].
  3. If need is in the map at a different index, return both.

The catch: it always reads the whole array twice and stores all n numbers, even when the answer is near the start.

4. Hash map, one pass TwoSumHashMapOnePass

Remembers: a map of the values seen so far.

38511001234never readivalueindex308152map after 3 stepsi = 0: need 6? no. store 3i = 1: need 1? no. store 8i = 2: need 4? no. store 5i = 3: need 8? yes, index 1return [1, 3]
  1. Work out need = target − nums[i].
  2. If need is already in the map, return [map[need], i].
  3. Otherwise store nums[i] → i and move on.

The catch: it still needs O(n) extra memory. Checking before storing is what stops a number from pairing with itself.

Four ways to solve it, step by step

Algorithm
Input

0comparisons
0map size
–need
–answer
nums
pairs (value, original index)left → right
HashMapvalue → index

current (i) partner (j) answer pair done

lines that run are highlighted

      

Complexity · array of n numbers

time
extra space

All four compared

AlgorithmTimeExtra spacePassesIdea

Related in the Lab HashMap in the Java Collections Field Guide

© 2026 Arman Ajabkhani · armantik.com · Privacy