Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

2Sum - Pair With Given Sum

Easy Acceptance 30.61% Points 20.00

Given an array arr of positive integers and another number target. Determine whether two elements exist in arr whose sum is exactly target or not. Return true if two elements exist in the arr else return false.

Examples
Example 1

Example 1

Input: arr[] = [1, 4, 9, 6, 10, 8], target =16

Output: true

Explanation: arr[3] + arr[4] = 6 + 10 = 16

Example 2

Example 2

Input: arr[] = [1, 2, 4, 3, 6], target = 11

Output: false

Explanation: None of the pair makes a sum of 11

Hints
Hint 1
Expected Time Complexity O(n)
Hint 2
Expected Space Complexity O(n)
Constraints
  • 1 <= arr.size <= 10^5
  • 1 <= arr[i] <= 10^5
Companies
Amazon Microsoft Adobe Google TCS + 5 more
Topics
Array Hashing Hashmap
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions