Javascript Program to Count pairs with given sum
Last Updated :
14 Feb, 2025
Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.
Note: Duplicate pairs are also allowed.
Examples:
Input : arr[] = {1, 5, 7, -1},
sum = 6
Output : 2
Pairs with sum 6 are (1, 5) and (7, -1)
Input : arr[] = {1, 5, 7, -1, 5},
sum = 6
Output : 3
Pairs with sum 6 are (1, 5), (7, -1) &
(1, 5)
Input : arr[] = {1, 1, 1, 1},
sum = 2
Output : 6
There are 3! pairs with sum 2.
Input : arr[] = {10, 12, 10, 15, -1, 7, 6,
5, 4, 2, 1, 1, 1},
sum = 11
Output : 9
Expected time complexity O(n)
Naive Solution :
A simple solution is to traverse each element and check if there's another number in the array which can be added to it to give sum.
JavaScript
// Javascript implementation of simple method to find count of
// pairs with given sum.
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
function getPairsCount(arr, n, sum) {
let count = 0; // Initialize result
// Consider all possible pairs and check their sums
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
if (arr[i] + arr[j] == sum)
count++;
return count;
}
// Driver function to test the above function
let arr = [1, 5, 7, -1, 5];
let n = arr.length;
let sum = 6;
console.log("Count of pairs is " + getPairsCount(arr, n, sum));
// This code is contributed by Mayank Tyagi
OutputCount of pairs is 3
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
Please refer complete article on Count pairs with given sum for more details!
Similar Reads
2 Sum - Count pairs with given sum Given an array arr[] of n integers and a target value, the task is to find the number of pairs of integers in the array whose sum is equal to target.Examples: Input: arr[] = {1, 5, 7, -1, 5}, target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (7, -1) & (1, 5). Input: arr[] = {1, 1, 1,
9 min read
Javascript Program for Number of pairs with maximum sum Write a javascript program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer i
3 min read
2 Sum - Count Pairs with given Sum in Sorted Array Given a sorted array arr[] and an integer target, the task is to find the number of pairs in the array whose sum is equal to target.Examples: Input: arr[] = [-1, 1, 5, 5, 7], target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (1, 5) and (-1, 7). Input: arr[] = [1, 1, 1, 1], target = 2Outpu
9 min read
Count Pairs from two arrays with even sum Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number. Note that an element will only be a part of a single pair.Examples:
7 min read
2 Sum - Count distinct pairs with given sum Given an array arr[] of size n and an integer target, the task is to count the number of distinct pairs in the array whose sum is equal to target.Examples:Input: arr[] = { 5, 6, 5, 7, 7, 8 }, target = 13 Output: 2 Explanation: Distinct pairs with sum equal to 13 are (5, 8) and (6, 7). Input: arr[] =
15 min read