Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Two Sum

LeetCode #1Easy
Unsorted · find a pair adding to target

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.

Asked atAmazonGoogleAppleMicrosoft
step 1 / 18
3
8
2
5
[0][1][2][3]
Brute force
1given arr, target
2for i ← 0 to n − 2:
3 for ji + 1 to n − 1:
4 if arr[i] + arr[j] == target:
5 return (i, j)
6return none
state
  • target10

line 1Find two indices i ≠ j with arr[i] + arr[j] = 10. The array is NOT sorted, so two-pointers won't work directly.