Example 2 : Check for pair in an array with a given sum

TopicDifficultyCompanies
Hash Table
MEDIUM
Google
Facebook
Amazon

Given an array arr of n integers and a number sum, write a program to check whether there is a pair of elements in the array that sums to exactly sum.

Problem Note

  • Any pair should consist of two different array elements.
  • Array arr may contain duplicates.
  • Return 1 if there is any such pair, else return 0.

Example 1

Input: arr[] = [-5, 1, -40, 20, 6, 8, 7 ], sum = 15
Output: 1
Explanation: (7, 8) and (-5, 20) are the pairs with sum 15.

Example 2

Input: arr[] = [-5, 4, -2, 16, 8, 9], sum = 15
Output: 0
Explanation: There is no pair of elements whose sum is equal to 15.

Code Editor

Practice and Learn

Best way to learn is through solving real problems. Practice this problem in this code editor.