Check if Pair with Given Sum Exists in Array in JavaScript
Last Updated :
07 May, 2024
Two Sum is a classic algorithmic problem where you're given an array of integers and a target sum. The task is to determine whether any two numbers in the array add up to the target sum.
Given an array A[] of n numbers and another number x, the task is to check whether or not there exist two elements in A[] whose sum is exactly x.
Examples:
Input: arr[] = [0, -1, 2, -3, 1], x= -2
Output: Yes
Explanation: If we calculate the sum of the output,1 + (-3) = -2
Input: arr[] = [1, -2, 1, 0, 5], x = 0
Output: No
Brute Force Approach
The brute force approach involves checking every possible pair of elements in the array to see if their sum equals the target. This approach has a time complexity of O(n^2).
Example: Implementation of brute force approach to check whether a pair with a given sum exists in an array.
JavaScript
function hasTwoSum(nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return true;
}
}
}
return false;
}
// Test
let nums = [2, 7, 11, 15];
let target = 9;
console.log(hasTwoSum(nums, target));
Time Complexity: O(n^2) - due to nested loops.
Space Complexity: O(1) - no extra space used.
Using Sorting + Two Pointers
Sort the array first, then use two pointers to traverse from the start and end simultaneously. Adjust the pointers based on whether the sum of the current pair is less than or greater than the target.
Example: Implementation of two pointer approach to check whether a pair with a given sum exists in an array.
JavaScript
function hasTwoSum(nums, target) {
nums.sort((a, b) => a - b);
let left = 0;
let right = nums.length - 1;
while (left < right) {
let sum = nums[left] + nums[right];
if (sum === target) {
return true;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}
// Test
let nums = [3, 5, 2, 8, 4];
let target = 10;
console.log(hasTwoSum(nums, target));
Time Complexity: O(nlogn) - due to sorting.
Space Complexity: O(1) - no extra space used apart from a few variables.
Using Hash Map
You can optimize the solution using a hash map (JavaScript object) to store the elements and their indices. While iterating through the array, check if the difference between the target and the current element exists in the hash map.
Example: Implementation of hashmap approach to check whether a pair with a given sum exists in an array.
JavaScript
function hasTwoSum(nums, target) {
let numMap = {};
for (let i = 0; i < nums.length; i++) {
let required = target - nums[i];
if (numMap[required]) {
return true;
}
numMap[nums[i]] = true;
}
return false;
}
// Test
let nums = [4, 9, 3, 2, 7];
let target = 11;
console.log(hasTwoSum(nums, target));
Time Complexity: O(n) - single pass through the array.
Space Complexity: O(n) - hashmap to store elements.
Similar Reads
Find a Pair with a Given Sum in BST using JavaScript Given a target sum and Binary Search Tree (BST), we have to write a function to find out the pair of nodes in BST whose values will sum up to this given sum. We must give these two numbers as an array that adds up to get this target sum. If there is no such number pair, return null. Example: Input:
4 min read
JavaScript Program to Find if Arrays are Disjoint or Not Given two arrays, Our task is to check if the given two arrays are disjoint or not. Disjoint arrays in JavaScript are arrays that have no elements in common Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = [2, 1, 3, 5]Output: Not Disjoint3 is common in two sets.Input: arr1[] = [12, 34, 11, 9, 3] arr2[] =
3 min read
JavaScript Program to Count Unequal Element Pairs from the Given Array Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.Examples:Input: arr[] = {6, 5, 2,
4 min read
Finding Sum of Alternate Elements of the Array using JavaScript Given an array, our task is to find the sum of alternative elements of the array using JavaScript. Example: Input: array = { 1, 2, 3, 4, 5, 6, 7, 8}Output: 16 Explanation:Sum of alternate elements of array = 1 + 3 + 5 + 7 = 16 Table of Content Using a for-loopUsing reduce Method Using Filter and Red
3 min read
JavaScript Program to Construct an Array from its pair-sum Array The pair-sum array is a unique construction that holds the sum of all potential pairs of elements from the original array. At first glance, it might appear to be challenging, but in this article, we'll Construct an array from its pair-sum array and discover some of its most intriguing uses. What is
4 min read
Sum of Distinct Elements of an Array using JavaScript One can find a Sum of distinct elements (unique or different numbers) present in an array using JavaScript. Below is an example to understand the problem clearly. Example:Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2] Output: 15 Explanation: The distinct elements present in array are: 1, 2, 3, 4 and 5 Sum = 1 +
4 min read