Find all pairs that sum to a target value in JavaScript

Our aim in the given problem statement is to find all the possible pairs which sum to a given target value with the help of Javascript. So we can solve this problem with the help of some built-in functions of Javascript.

Understanding the Problem

The given problem is stating that we have given an array and a target value and our task is to find all the possible pairs that sum to the target value. For example suppose we have an array like [6, 5, 4, 3, 2, 1] and the target value is 6 so the possible pairs will be [ [ 2, 4 ], [ 1, 5 ] ].

Logic for the given problem

We will start by initializing the blank array to store the pairs which satisfy the condition. After that we will use a map object to keep track of every number from the input array. Then with the help of a loop we will iterate over the array items. In every iteration we will calculate the complement of the current number by subtracting the target value. If we have found the complement, save it in the map object. After adding all the pairs to the result array we will update the map object's frequency of the current number. And return the pairs array which will contain all the possible pairings with sums equal to the goal value.

Algorithm

Step 1: With the help of an empty array we will store the target values of pairs that sum to the given target.

Step 2: Create a map object to store the tracking of the frequency of every number in the input array.

Step 3: Traverse over the numbers in the input array.

Step 4: Calculate the complement of the numbers by subtracting it from the target value.

Step 5: If the complement is present of the number in the map then we will iterate complement count and add to the pairs array.

Step 6: Update the frequency of the number in the map object. And return the pairs array to the console.

Example

// Function to find pairs of the given target from the array
function findPossiblePairs(arr, target) {
const pairs = [];
const map = {};
for (let i = 0; i < arr.length; i++) {
const complement = target - arr[i];

if (map.hasOwnProperty(complement)) {
const compCount = map[complement];

for (let j = 0; j < compCount; j++) {
pairs.push([arr[i], complement]);
}
}

if (map.hasOwnProperty(arr[i])) {
map[arr[i]]++;
} else {
map[arr[i]] = 1;
}
}

return pairs;
}
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = 10;

const pairs = findPossiblePairs(nums, sum);
console.log(pairs);


Output

[ [ 6, 4 ], [ 7, 3 ], [ 8, 2 ], [ 9, 1 ] ]


Complexity

The time for execution of the code is O(n), here n is the size of the given input array. As we have iterated over the array once. And we have used an object map to store the frequency count of the integers in the array; the space complexity is O(n).

Conclusion

In the linear time the code we have created identifies all the possible pairs that sum to the desired value. We have also determined the complements of every item or number with usage of frequency map and create the possible pairs. Without using the nested loops we have achieved the desired output with minimal time and space complexity.

Updated on: 14-Aug-2023

1K+ Views