Two Sum
Easy 20 XPArraysHashing
Commonly asked at Google, Amazon, Microsoft, Facebook
Read a list of integers and a target. Find the two different positions i < j whose values add up to the target and print i j (0-based). Exactly one answer exists.
- Input
- Line 1: space-separated integers. Line 2: target.
- Output
- Two indices i j.
- Constraints
- Up to 100,000 numbers. Aim for O(n).
Sample 1
Input
2 7 11 15 9
Output
0 1
Explanation: 2 + 7 = 9
Read input with input() (no prompt message) and print only the answer. Run tries the Input box; Submit checks all test cases.
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.The solution unlocks after 3 submissions.