All problems
easy · 421 tests
Two Sum
You are given an array of n integers and a target k. Print the indices (0-based, smaller first) of the two distinct elements whose sum is exactly k. Exactly one such pair exists in every case.
Input
Line 1: t, the number of cases (1 <= t <= 1000).
Then t cases, each two lines:
n k(2 <= n <= 10^5,-10^9 <= k <= 10^9)nintegers, each in[-10^9, 10^9]
The total of n over one input is at most 10^6.
Output
For each case, one line: two integers i j with i < j, separated by a space.
Note
An element may not be used twice: for [3, 5] and k = 6 there is no answer from 3 + 3. An O(n^2) solution will time out on the larger tests.
Samples
Input 1
1 4 9 2 7 11 15
Output 1
0 1
Input 2
1 3 6 3 2 4
Output 2
1 2
Input 3
1 2 6 3 3
Output 3
0 1
Input 4
1 5 -8 -1 -2 -3 -4 -5
Output 4
2 4
Input 5
1 4 0 0 4 3 0
Output 5
0 3
Scoring
- Basic correctness20 pts
- Edge cases15 pts
- Random small20 pts
- Random large15 pts
- Adversarial10 pts
- Performance20 pts
2s · 256 MB