2 Sum - Count Pairs with given Sum in Sorted Array
Last Updated :
12 Jul, 2025
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 = 6
Output: 3
Explanation: Pairs with sum 6 are (1, 5), (1, 5) and (-1, 7).
Input: arr[] = [1, 1, 1, 1], target = 2
Output: 6
Explanation: Pairs with sum 2 are (1, 1), (1, 1), (1, 1), (1, 1), (1, 1) and (1, 1).
Input: arr[] = [-1, 10, 10, 12, 15], target = 125
Output: 0
In this post, we are counting pairs with given sum when the input array is sorted. To count the pairs when the input array is not sorted, refer to 2 Sum – Count pairs with given sum.
[Naive Approach] By Generating All Possible Pairs - O(n^2) time and O(1) space
The very basic approach is to generate all the possible pairs and check if any pair exists whose sum is equals to given target value, then increment the count variable.
To know more about the implementation, please refer 2 Sum – Count pairs with given sum.
[Expected Approach] Using Two Pointer Technique - O(n) and O(1) Space
The idea is to use the two-pointer technique by maintaining two pointers, say left and right and initialize them to the first and last element of the array respectively. According to the sum of left and right pointers, we can have three cases:
- arr[left] + arr[right] < target: We need to increase the sum of pair, move the left pointer towards right.
- arr[left] + arr[right] > target: We need to decrease the sum of pair, move the right pointer towards left.
- arr[left] + arr[right] = target: We have found a pair whose sum is equal to target. We can find the product of the count of both the elements and add them to the result.
C++
// C++ program for counting pairs with given sum
// using Two Pointer Technique
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int countPairs(vector<int> &arr, int target) {
int res = 0;
int n = arr.size();
int left = 0, right = n - 1;
while (left < right) {
// If sum is greater
if (arr[left] + arr[right] < target)
left++;
// If sum is lesser
else if (arr[left] + arr[right] > target)
right--;
// If sum is equal
else {
int cnt1 = 0, cnt2 = 0;
int ele1 = arr[left], ele2 = arr[right];
// Count frequency of first element of the pair
while (left <= right and arr[left] == ele1) {
left++;
cnt1++;
}
// Count frequency of second element of the pair
while(left <= right and arr[right] == ele2) {
right--;
cnt2++;
}
// If both the elements are same, then count of
// pairs = the number of ways to choose 2
// elements among cnt1 elements
if(ele1 == ele2)
res += (cnt1 * (cnt1 - 1))/2;
// If the elements are different, then count of
// pairs = product of the count of both elements
else
res += (cnt1 * cnt2);
}
}
return res;
}
int main() {
vector<int> arr = {-1, 1, 5, 5, 7};
int target = 6;
cout << countPairs(arr, target);
return 0;
}
C
// C program for counting pairs with given sum
// using Two Pointer Technique
#include <stdio.h>
// Function to return the count of pairs
int countPairs(int arr[], int n, int target) {
int res = 0;
int left = 0, right = n - 1;
while (left < right) {
// If sum is greater
if (arr[left] + arr[right] < target)
left++;
// If sum is lesser
else if (arr[left] + arr[right] > target)
right--;
// If sum is equal
else {
int cnt1 = 0, cnt2 = 0;
int ele1 = arr[left], ele2 = arr[right];
// Count frequency of first element of the pair
while (left <= right && arr[left] == ele1) {
left++;
cnt1++;
}
// Count frequency of second element of the pair
while(left <= right && arr[right] == ele2) {
right--;
cnt2++;
}
// If both the elements are same, then count of
// pairs = the number of ways to choose 2
// elements among cnt1 elements
if(ele1 == ele2)
res += (cnt1 * (cnt1 - 1)) / 2;
// If the elements are different, then count of
// pairs = product of the count of both elements
else
res += (cnt1 * cnt2);
}
}
return res;
}
int main() {
int arr[] = {-1, 1, 5, 5, 7};
int target = 6;
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", countPairs(arr, n, target));
return 0;
}
Java
// Java program for counting pairs with given sum
// using Two Pointer Technique
import java.util.Arrays;
class GfG {
// Function to return the count of pairs
static int countPairs(int[] arr, int target) {
int res = 0;
int n = arr.length;
int left = 0, right = n - 1;
while (left < right) {
// If sum is greater
if (arr[left] + arr[right] < target)
left++;
// If sum is lesser
else if (arr[left] + arr[right] > target)
right--;
// If sum is equal
else {
int cnt1 = 0, cnt2 = 0;
int ele1 = arr[left], ele2 = arr[right];
// Count frequency of first element of the pair
while (left <= right && arr[left] == ele1) {
left++;
cnt1++;
}
// Count frequency of second element of the pair
while (left <= right && arr[right] == ele2) {
right--;
cnt2++;
}
// If both the elements are same, then count of
// pairs = the number of ways to choose 2
// elements among cnt1 elements
if (ele1 == ele2)
res += (cnt1 * (cnt1 - 1)) / 2;
// If the elements are different, then count of
// pairs = product of the count of both elements
else
res += (cnt1 * cnt2);
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {-1, 1, 5, 5, 7};
int target = 6;
System.out.println(countPairs(arr, target));
}
}
Python
# Python program for counting pairs with given sum
# using Two Pointer Technique
def countPairs(arr, target):
res = 0
n = len(arr)
left = 0
right = n - 1
while left < right:
# If sum is greater
if arr[left] + arr[right] < target:
left += 1
# If sum is lesser
elif arr[left] + arr[right] > target:
right -= 1
# If sum is equal
else:
cnt1 = 0
cnt2 = 0
ele1 = arr[left]
ele2 = arr[right]
# Count frequency of first element of the pair
while left <= right and arr[left] == ele1:
left += 1
cnt1 += 1
# Count frequency of second element of the pair
while left <= right and arr[right] == ele2:
right -= 1
cnt2 += 1
# If both the elements are same, then count of
# pairs = the number of ways to choose 2
# elements among cnt1 elements
if ele1 == ele2:
res += (cnt1 * (cnt1 - 1)) // 2
# If the elements are different, then count of
# pairs = product of the count of both elements
else:
res += (cnt1 * cnt2)
return res
if __name__ == "__main__":
arr = [-1, 1, 5, 5, 7]
target = 6
print(countPairs(arr, target))
C#
// C# program for counting pairs with given sum using
// Two Pointer Technique
using System;
class GfG {
// Function to return the count of pairs
static int countPairs(int[] arr, int target) {
int res = 0;
int n = arr.Length;
int left = 0, right = n - 1;
while (left < right) {
// If sum is greater
if (arr[left] + arr[right] < target)
left++;
// If sum is lesser
else if (arr[left] + arr[right] > target)
right--;
// If sum is equal
else {
int cnt1 = 0, cnt2 = 0;
int ele1 = arr[left], ele2 = arr[right];
// Count frequency of first element of the pair
while (left <= right && arr[left] == ele1) {
left++;
cnt1++;
}
// Count frequency of second element of the pair
while (left <= right && arr[right] == ele2) {
right--;
cnt2++;
}
// If both the elements are same, then count of
// pairs = the number of ways to choose 2
// elements among cnt1 elements
if (ele1 == ele2)
res += (cnt1 * (cnt1 - 1)) / 2;
// If the elements are different, then count of
// pairs = product of the count of both elements
else
res += (cnt1 * cnt2);
}
}
return res;
}
static void Main(string[] args) {
int[] arr = { -1, 1, 5, 5, 7 };
int target = 6;
Console.WriteLine(countPairs(arr, target));
}
}
JavaScript
// JavaScript program for counting pairs with given sum
// using Two Pointer Technique
function countPairs(arr, target) {
let res = 0;
const n = arr.length;
let left = 0, right = n - 1;
while (left < right) {
// If sum is greater
if (arr[left] + arr[right] < target) {
left++;
}
// If sum is lesser
else if (arr[left] + arr[right] > target) {
right--;
}
// If sum is equal
else {
let cnt1 = 0, cnt2 = 0;
const ele1 = arr[left], ele2 = arr[right];
// Count frequency of first element of the pair
while (left <= right && arr[left] === ele1) {
left++;
cnt1++;
}
// Count frequency of second element of the pair
while (left <= right && arr[right] === ele2) {
right--;
cnt2++;
}
// If both the elements are same, then count of
// pairs = the number of ways to choose 2
// elements among cnt1 elements
if (ele1 === ele2)
res += (cnt1 * (cnt1 - 1)) / 2;
// If the elements are different, then count of
// pairs = product of the count of both elements
else
res += (cnt1 * cnt2);
}
}
return res;
}
// Driver Code
const arr = [-1, 1, 5, 5, 7];
const target = 6;
console.log(countPairs(arr, target));
Similar Reads
3 Sum - Count Triplets With Given Sum In Sorted Array Given a sorted array arr[] and a target value, the task is to find the count of triplets present in the given array having sum equal to the given target. More specifically, the task is to count triplets (i, j, k) of valid indices, such that arr[i] + arr[j] + arr[k] = target and i < j < k.Examp
10 min read
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
Count number of pairs with positive sum in an array Given an array arr[] of N integers, the task is to count the number of pairs with positive sum. Examples: Input: arr[] = {-7, -1, 3, 2} Output: 3 Explanation: The pairs with positive sum are: {-1, 3}, {-1, 2}, {3, 2}. Input: arr[] = {-4, -2, 5} Output: 2 Explanation: The pairs with positive sum are:
8 min read
Count number of pairs with positive sum in an array Given an array arr[] of N integers, the task is to count the number of pairs with positive sum. Examples: Input: arr[] = {-7, -1, 3, 2} Output: 3 Explanation: The pairs with positive sum are: {-1, 3}, {-1, 2}, {3, 2}. Input: arr[] = {-4, -2, 5} Output: 2 Explanation: The pairs with positive sum are:
8 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