Open In App

2 Sum - Count Pairs with given Sum in Sorted Array

Last Updated : 12 Jul, 2025
Suggest changes
Share
22 Likes
Like
Report

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)); 

Output
3



Explore