Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

2Sum - Pair With Given Sum

Easy Acceptance 30.61% Points 20

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

Constraints
  • 1 <= arr.size <= 10^5
  • 1 <= arr[i] <= 10^5
Hints
  • Expected Time Complexity O(n)
  • Expected Space Complexity O(n)
Topics
Array Hashing Hashmap
Companies
Amazon Microsoft Adobe Google TCS Flipkart Infosys Morgan Stanly Zoho Wipro
Added 19 Jul 2026 30.61% acceptance
Ln 1, Col 1Sign in to save your code
16
1 4 9 6 10 8
true