1 Answers
Finding Pairs of Numbers Adding up to a Target Sum
When provided with an array of integers, we can create a function to identify all pairs of numbers that add up to a specific target sum. Here is one way to achieve this:
<script type="text/javascript">
function findPairs(array, targetSum) {
let pairs = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === targetSum) {
pairs.push([array[i], array[j]]);
}
}
}
return pairs;
}
</script>
By using this function with the given array and target sum, we can efficiently find and return all pairs that meet the criteria.
Please login or Register to submit your answer