← All coding problems
Two Sum
easyArrays & Hashing+10 XPTime limit 2s
Given an array of n integers and a target, find the two different positions i < j such that a[i] + a[j] = target. Exactly one such pair exists.
Input:
- line 1:
n - line 2:
nintegersa[0] … a[n−1] - line 3:
target
Output: i j (0-based, i < j).
Constraints: 2 ≤ n ≤ 10⁵, −10⁹ ≤ a[i], target ≤ 10⁹. An O(n²) solution is too slow for the largest tests — use a hash map.
Sample input 1
4 2 7 11 15 9
Sample output 1
0 1
Sample input 2
3 3 2 4 6
Sample output 2
1 2
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).