Count pairs with given sum | Set 2
Given an array arr[] and an integer sum, the task is to find the number of pairs of integers in the array whose sum is equal to sum.
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
Approach: Two Different methods have already been discussed here. Here, a method based on sorting will be discussed.
- Sort the array and take two pointers i and j, one pointer pointing to the start of the array i.e. i = 0 and another pointer pointing to the end of the array i.e. j = n – 1.
-
- Greater than the sum then decrement j.
- Lesser than the sum then increment i.
- Equals to the sum then count such pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int pairs_count( int arr[], int n, int sum)
{
int ans = 0;
sort(arr, arr + n);
int i = 0, j = n - 1;
while (i < j) {
if (arr[i] + arr[j] < sum)
i++;
else if (arr[i] + arr[j] > sum)
j--;
else {
int x = arr[i], xx = i;
while (i < j and arr[i] == x)
i++;
int y = arr[j], yy = j;
while (j >= i and arr[j] == y)
j--;
if (x == y) {
int temp = i - xx + yy - j - 1;
ans += (temp * (temp + 1)) / 2;
}
else
ans += (i - xx) * (yy - j);
}
}
return ans;
}
int main()
{
int arr[] = { 1, 5, 7, 5, -1 };
int n = sizeof (arr) / sizeof (arr[0]);
int sum = 6;
cout << pairs_count(arr, n, sum);
return 0;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int pairs_count( int arr[], int n, int sum)
{
int ans = 0 ;
Arrays.sort(arr);
int i = 0 , j = n - 1 ;
while (i < j)
{
if (arr[i] + arr[j] < sum)
i++;
else if (arr[i] + arr[j] > sum)
j--;
else
{
int x = arr[i], xx = i;
while ((i < j ) && (arr[i] == x))
i++;
int y = arr[j], yy = j;
while ((j >= i )&& (arr[j] == y))
j--;
if (x == y)
{
int temp = i - xx + yy - j - 1 ;
ans += (temp * (temp + 1 )) / 2 ;
}
else
ans += (i - xx) * (yy - j);
}
}
return ans;
}
public static void main (String[] args)
{
int arr[] = { 1 , 5 , 7 , 5 , - 1 };
int n = arr.length;
int sum = 6 ;
System.out.println (pairs_count(arr, n, sum));
}
}
|
Python3
def pairs_count(arr, n, sum ):
ans = 0
arr = sorted (arr)
i, j = 0 , n - 1
while (i < j):
if (arr[i] + arr[j] < sum ):
i + = 1
elif (arr[i] + arr[j] > sum ):
j - = 1
else :
x = arr[i]
xx = i
while (i < j and arr[i] = = x):
i + = 1
y = arr[j]
yy = j
while (j > = i and arr[j] = = y):
j - = 1
if (x = = y):
temp = i - xx + yy - j - 1
ans + = (temp * (temp + 1 )) / / 2
else :
ans + = (i - xx) * (yy - j)
return ans
arr = [ 1 , 5 , 7 , 5 , - 1 ]
n = len (arr)
sum = 6
print (pairs_count(arr, n, sum ))
|
C#
using System;
class GFG
{
static int pairs_count( int []arr,
int n, int sum)
{
int ans = 0;
Array.Sort(arr);
int i = 0, j = n - 1;
while (i < j)
{
if (arr[i] + arr[j] < sum)
i++;
else if (arr[i] + arr[j] > sum)
j--;
else
{
int x = arr[i], xx = i;
while ((i < j) && (arr[i] == x))
i++;
int y = arr[j], yy = j;
while ((j >= i) && (arr[j] == y))
j--;
if (x == y)
{
int temp = i - xx + yy - j - 1;
ans += (temp * (temp + 1)) / 2;
}
else
ans += (i - xx) * (yy - j);
}
}
return ans;
}
public static void Main (String[] args)
{
int []arr = { 1, 5, 7, 5, -1 };
int n = arr.Length;
int sum = 6;
Console.WriteLine (pairs_count(arr, n, sum));
}
}
|
Javascript
<script>
function pairs_count(arr, n, sum)
{
let ans = 0;
arr.sort();
let i = 0, j = n - 1;
while (i < j) {
if (arr[i] + arr[j] < sum)
i++;
else if (arr[i] + arr[j] > sum)
j--;
else {
let x = arr[i], xx = i;
while (i < j && arr[i] == x)
i++;
let y = arr[j], yy = j;
while (j >= i && arr[j] == y)
j--;
if (x == y) {
let temp = i - xx + yy - j - 1;
ans += (temp * (temp + 1)) / 2;
}
else
ans += (i - xx) * (yy - j);
}
}
return ans;
}
let arr = [ 1, 5, 7, 5, -1 ];
let n = arr.length;
let sum = 6;
document.write(pairs_count(arr, n, sum));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Last Updated :
07 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...