Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Two Sum (LeetCode #1)

Two Sum (LeetCode problem #1) asks for the two indices in an array nums whose values add up to target. The one-pass hash-map solution walks the array once, checking at each element whether its complement has already been seen, cutting the time complexity from the brute-force O(n²) to O(n).

Think of a doorman with a guest list. As each guest arrives, he checks the list first for the partner who completes the pair; if that partner is already inside, the match is done, and otherwise he adds the newcomer to the list. The list is the hash map, and checking before writing is the core of the algorithm.

How Two Sum Works

Walk the array left to right while maintaining a dictionary seen that maps each value to its index. At position i, compute complement = target - nums[i], the partner value this element needs. Check whether the complement is already in seen; if it is, return [seen[complement], i], earlier index first. Only after the check fails do you insert the current element: seen[nums[i]] = i.

Checking the map before inserting the current element prevents self-matching. When nums[i] equals target / 2, a lookup after insertion would find the element just written and pair it with itself, which the problem forbids. Checking against the map as it stood before this iteration guarantees an element can never pair with itself.

The naive alternative tests every pair (i, j) with j > i in a nested loop: no dictionary, n(n−1)/2 candidate pairs, O(n²) time. Both solutions are animated here: the Approach toggle in the Inputs panel switches between Hash map (default) and Brute force on the same input.

Two Sum Step by Step

The default input is nums = [2, 7, 11, 15] with target = 18, using the hash-map approach:

  1. i = 0, nums[i] = 2. complement = 18 − 2 = 16. Is 16 in seen? No, the map is empty. Insert seen[2] = 0.
  2. i = 1, nums[i] = 7. complement = 18 − 7 = 11. Not in seen. Insert seen[7] = 1. The map is now {2: 0, 7: 1}.
  3. i = 2, nums[i] = 11. complement = 18 − 11 = 7. Is 7 in seen? Yes, at index 1. Return [1, 2]. Index 3 (value 15) is never inspected.

Flipping the toggle to brute force takes four pair checks on the same input: 2 + 7 = 9, 2 + 11 = 13, 2 + 15 = 17, and 7 + 11 = 18, returning [1, 2]. Both approaches produce the same answer, while the checks counter in the header grows quadratically under brute force and linearly under the hash map.

In the visualization, the array sits on the right and seen builds up as a two-column "value | index" panel on the left. Each iteration has four beats: the active cell glows, the complement is computed, the dictionary is probed, and the element either matches (the dict row and active cell turn green together) or is written into seen (as a new row, or overwriting an existing row when the value repeats). In brute-force mode the seen panel disappears because no lookup table is maintained.

Time and Space Complexity

Brute forceHash map
Best caseO(1), first pair matchesO(1), pair at indices 0, 1
Worst caseO(n²)O(n) expected (O(1) hash lookups)
SpaceO(1)O(n)

The extra space buys speed: the hash map spends O(n) memory to answer whether a complement has appeared in constant expected time instead of rescanning earlier elements. At the default n = 4, the two approaches take a comparable number of beats; increasing the input size makes the brute-force checks counter pull away while the hash map scales linearly.

Edge Cases

The player accepts 3–8 values in the range 1–99, so two of these cases are worth tracing on paper.

  • No solution. LeetCode guarantees one answer, while the displayed code falls off the end of the loop (returning None in Python). The animation displays an explicit "no solution" beat rather than failing; production code should handle that case explicitly.
  • Two equal values. For nums = [3, 3, 4] and target = 6, at i = 1 the complement 3 is already in seen from index 0, returning [0, 1]. This works only because the lookup runs before the insert.
  • Duplicates not in the answer. A repeated value overwrites its row in seen. That is safe because the earlier index's own complement check has already run before its entry is overwritten.
  • Negatives and zero. A hash map keys on any integer, so negative values and zero need no special handling, though the player's input range keeps them off the canvas.

Common Mistakes

  • Insert before check. The most common bug. It looks symmetric, yet lets target / 2 match the element just inserted, pairing an element with itself.
  • Returning values instead of indices. The problem asks for positions. The map must store indices as its values, not the numbers themselves.
  • Sorting first to use two pointers. Sorting discards the original indices; recovering them requires carrying each value's original position along, adding more bookkeeping than the hash map needs.
  • Using a set instead of a map. A set answers whether a value is present, not which index holds it, and the index is the required output.