Learn
Exam

Find a Specific Sum in an Array 2

Difficulty
BEGINNER

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 begin:math:textbegin:math:text2 + 6 = 8end:math:textend:math:text or begin:math:textbegin:math:text3 + 5 = 8end:math:textend:math:text, 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 numbers is between 2 and 100,000 inclusive.
  • Each element of numbers is a distinct natural number between 1 and 1,000,000 inclusive.
  • target is 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 numbers yields 8, so the function returns true.

Explanation for Test Case 2

  • No pair of elements in numbers can sum to 6, so the function returns false.

Explanation for Test Case 3

  • Adding 1 and 2 in numbers yields 3, so the function returns true.