3
8
2
5
[0][1][2][3]
Brute force
▸1given arr, target2for i ← 0 to n − 2:3 for j ← i + 1 to n − 1:4 if arr[i] + arr[j] == target:5 return (i, j)6return none
state
- target10
warming up the animation
Given an array of integers and a target, return the indices of the two numbers that add up to the target. Exactly one solution exists and you may not reuse an element.
▸1given arr, target2for i ← 0 to n − 2:3 for j ← i + 1 to n − 1:4 if arr[i] + arr[j] == target:5 return (i, j)6return none
line 1Find two indices i ≠ j with arr[i] + arr[j] = 10. The array is NOT sorted, so two-pointers won't work directly.