OneCompiler

Problem Solving

Given a list of integers find a pair of integers which add up to given input sum

1 Answer

2 years ago by

Using JavaScript

function findPairWithSum(arr, targetSum) {

    for (let i = 0; i < arr.length; i++) {
  
        for (let j = i + 1; j < arr.length; j++) {
        
            if (arr[i] + arr[j] === targetSum) {
                return [arr[i], arr[j]];  // Return the pair if sum matches target
            }
            
        }
    }
    return null;  // Return null if no pair is found
}


// Example usage:
const arr = [10, 2, 3, 5, 7, 1, 8];
const targetSum = 10;
console.log(findPairWithSum(arr, targetSum));  // Output: [3, 7]
1 year ago by Sandeep Chauhan