Given an array of integers, write a function to return all pairs of numbers in the array that add up to a specific target sum.

1 Answers
Answered by suresh

Interview Question: Find Pairs of Numbers Adding up to a Target Sum

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.

Answer for Question: Given an array of integers, write a function to return all pairs of numbers in the array that add up to a specific target sum.