Guest User

Untitled

a guest
Jun 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /*
  2. QUESTION:
  3. Given an array of integers, and a number ‘sum’,
  4. return a Boolean denoting whether there is a pair
  5. of integers in the array whose sum is equal to ‘sum’.
  6. NOTE:
  7. Expected time complexity O(n)
  8. */
  9. const arr = [-1,1,2,5,3,4,5]
  10. const hasPair = (arr, sum) => {
  11. const comps = new Set() // set of complimenting pairs
  12. return arr.some(item => comps.has(item) || (comps.add(sum-item), false))
  13. }
  14. hasPair(arr, -1) // false
  15. hasPair(arr, 0) // true
  16. hasPair(arr, 8) // true
  17. hasPair(arr, 10) // true
  18. hasPair(arr, 11) // false
Add Comment
Please, Sign In to add comment