Problem
Given a one-dimensional array of distinct natural numbers numbers and a natural number target, write a function solution that returns whether there exists a pair of two different elements in numbers whose sum is exactly equal to target.
For example, if numbers is [2, 5, 6, 3] and target is 8, then 2 + 6 = 8 or 3 + 5 = 8, so the function returns true.
On the other hand, if target is 6, no pair of two numbers can sum to 6, so the function returns false.
Constraints
- The length of
numbersis between 2 and 1,000 inclusive. - Each element of
numbersis a distinct natural number between 1 and 1,000,000 inclusive. targetis a natural number between 1 and 2,000,000 inclusive.
Test Case
| Test Case Number | numbers | target | return |
|---|---|---|---|
| 1 | [2,5,6,3] | 8 | true |
| 2 | [2,5,6,3] | 6 | false |
| 3 | [1,2,3,4] | 3 | true |
Explanation for Test Case 1
- Adding 2 and 6, or 3 and 5 in
numbersyields 8, so the function returnstrue.
Explanation for Test Case 2
- No pair of elements in
numberscan sum to 6, so the function returnsfalse.
Explanation for Test Case 3
- Adding 1 and 2 in
numbersyields 3, so the function returnstrue.