Find Pair with Target Sum

Medium~10 min

Given an array of integers nums and an integer target, return true if there exist two distinct indices i and j such that nums[i] + nums[j] == target, and false otherwise.

Important: Your solution must run in O(n) time.

This is similar to Two Sum, but you only need to return whether a pair exists (boolean), not the actual indices.

Examples

Example 1
Input: nums = [1, 5, 3, 7], target = 8
Output: true
Explanation: nums[0] + nums[3] = 1 + 7 = 8.
Example 2
Input: nums = [2, 4, 6], target = 5
Output: false
Explanation: No two elements sum to 5.
Example 3
Input: nums = [3, 3], target = 6
Output: true
Explanation: nums[0] + nums[1] = 3 + 3 = 6.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Expected time complexity: O(n)
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run