Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Pair With Sum Exists

Hash Mapseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 124: Maps: Fundamentals

Implement `hasPairWithSum(nums, target)` returning whether two distinct elements add up to `target`, using a set for O(n) lookups.

Sample tests

Input: hasPairWithSum([1,2,3], 5)
Output: true
Input: hasPairWithSum([1,2], 10)
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Checking the complement before considering distinctness; adding after the check handles it.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

A running set lets you check each complement in O(1), giving O(n) time overall.

Loading...
⌘/Ctrl + Enter

Run your code to see results.