Tasks /
Algorithms /
Two numbers with a given sum
Statement
medium
Two numbers with a given sum
Implement `two_sum(numbers, target)`: return the indices of two numbers that add
up to `target`, or an empty list when there is no such pair.
The solution is guaranteed to be unique and the same element cannot be used
twice.
Example: `two_sum([2, 7, 11, 15], 9)` → `[0, 1]`.
The expected solution is one pass with a "value → index" dict.
AI mentor
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and ask
Hints
1 of 3 unlocked · attempts: 0
Hint 1
What do you need to remember as you go, so that you do not re-check every pair
from scratch?
Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts