Pair whose sum exists in the array in JavaScript


In this article we will see how to find a pair of items in an array which has sum equals to a specific target number. So we will use JavaScript to implement the algorithm.

Understanding the Problem

The problem at hand is to find the pair of numbers in the array whose sum is equal to the given target value. And the target value should also be there in the array. Or we can say that we have to identify pairs (a, b) where a + b = c and c is also present in the array.

Logic for the given Problem

To solve this problem we will use a hash set to store the items of the array. So we need to traverse through the item in the array and we will check the condition that if the difference between the current item and any other item is present in the hash set. If the condition is satisfied then we have found the pair whose sum is present in the array.

Algorithm

Step 1: As we have to find out the pair of items whose sum is equal to the given target value and the target value should also be present in the array. So for doing this task we will create a function. In this function we will pass an argument of array as arr.

Step 2: After creating the function we are required to initialize a blank hash set to store the resultant array of pairs.

Step 3: Now we have a function and a variable to store the result. After this we will use a loop to iterate through every item in the given array.

Step 4: In this loop we will check the condition, if the difference between num and any other element is present in the hash set. So if the difference is present in the set then will add the pair to the result variable.

Step 5: After the above step we will add num to the hash set and return the result. The result will contain the pairs whose sum is present in the given array.

Example

// Function for finding pairs with the sum
function findPairsWithSumExists(arr) {
   const hashSet = new Set();
   const result = [];

   for (let i = 0; i < arr.length; i++) {
      const num = arr[i];

      for (let j = 0; j < arr.length; j++) {
         if (j === i) continue; // Skip the same element

         const diff = num - arr[j];
         if (hashSet.has(diff)) {
            result.push([num, arr[j]]);
         }
      }

      hashSet.add(num);
   }

   return result;
}

const arr = [1, 2, 3, 4, 5];
const pairs = findPairsWithSumExists(arr);
console.log(pairs);

Output

[
   [ 2, 1 ], [ 3, 1 ],
   [ 3, 2 ], [ 4, 1 ],
   [ 4, 2 ], [ 4, 3 ],
   [ 5, 1 ], [ 5, 2 ],
   [ 5, 3 ], [ 5, 4 ]
]

Complexity

The time complexity for finding the pairs of numbers whose sum is equal to the given target is O(n^2), here n is the size of the given array. The reason for this complexity is that we have used a nested loop to iterate the array. And the space complexity for the code is O(n) because we have utilized a hash set to store the result.

Conclusion

As we have produced the code for finding the pair as per the given problem. By using the hash set to store the item of the array, we can effectively find the pairs of numbers whose sum is present in the array. And the algorithm has a quadratic time complexity with linear space complexity.

Updated on: 16-Aug-2023

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements