Count pairs from two sorted arrays whose sum is equal to a given value x
Last Updated :
29 Mar, 2024
Given two sorted arrays of size m and n of distinct elements. Given a value x. The problem is to count all pairs from both arrays whose sum is equal to x.
Note: The pair has an element from each array.
Examples :
Input : arr1[] = {1, 3, 5, 7}
arr2[] = {2, 3, 5, 8}
x = 10
Output : 2
The pairs are:
(5, 5) and (7, 3)
Input : arr1[] = {1, 2, 3, 4, 5, 7, 11}
arr2[] = {2, 3, 4, 5, 6, 8, 12}
x = 9
Output : 5
Method 1 (Naive Approach): Using two loops pick elements from both the arrays and check whether the sum of the pair is equal to x or not.
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0;
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
if ((arr1[i] + arr2[j]) == x)
count++;
return count;
}
int main()
{
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 3, 5, 8};
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
int x = 10;
cout << "Count = "
<< countPairs(arr1, arr2, m, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPairs( int []arr1,
int []arr2, int m, int n, int x)
{
int count = 0 ;
for ( int i = 0 ; i < m; i++)
for ( int j = 0 ; j < n; j++)
if ((arr1[i] + arr2[j]) == x)
count++;
return count;
}
public static void main (String[] args)
{
int arr1[] = { 1 , 3 , 5 , 7 };
int arr2[] = { 2 , 3 , 5 , 8 };
int m = arr1.length;
int n = arr2.length;
int x = 10 ;
System.out.println( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
Python3
def countPairs(arr1, arr2, m, n, x):
count = 0
for i in range (m):
for j in range (n):
if arr1[i] + arr2[j] = = x:
count = count + 1
return count
arr1 = [ 1 , 3 , 5 , 7 ]
arr2 = [ 2 , 3 , 5 , 8 ]
m = len (arr1)
n = len (arr2)
x = 10
print ( "Count = " ,
countPairs(arr1, arr2, m, n, x))
|
C#
using System;
class GFG {
static int countPairs( int []arr1,
int []arr2, int m, int n, int x)
{
int count = 0;
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
if ((arr1[i] + arr2[j]) == x)
count++;
return count;
}
public static void Main ()
{
int []arr1 = {1, 3, 5, 7};
int []arr2 = {2, 3, 5, 8};
int m = arr1.Length;
int n = arr2.Length;
int x = 10;
Console.WriteLine( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
PHP
<?php
function countPairs( $arr1 , $arr2 ,
$m , $n , $x )
{
$count = 0;
for ( $i = 0; $i < $m ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
if (( $arr1 [ $i ] + $arr2 [ $j ]) == $x )
$count ++;
return $count ;
}
$arr1 = array (1, 3, 5, 7);
$arr2 = array (2, 3, 5, 8);
$m = count ( $arr1 );
$n = count ( $arr2 );
$x = 10;
echo "Count = " ,
countPairs( $arr1 , $arr2 ,
$m , $n , $x );
?>
|
Javascript
<script>
function countPairs(arr1, arr2, m, n, x)
{
let count = 0;
for (let i = 0; i < m; i++)
for (let j = 0; j < n; j++)
if ((arr1[i] + arr2[j]) == x)
count++;
return count;
}
let arr1 = [1, 3, 5, 7];
let arr2 = [2, 3, 5, 8];
let m = arr1.length;
let n = arr2.length;
let x = 10;
document.write( "Count = "
+ countPairs(arr1, arr2, m, n, x));
</script>
|
Output :
Count = 2
Time Complexity : O(mn)
Auxiliary space : O(1)
Method 2 (Binary Search): For each element arr1[i], where 1 <= i <= m, search the value (x – arr1[i]) in arr2[]. If search is successful, increment the count.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPresent( int arr[], int low,
int high, int value)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == value)
return true ;
else if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return false ;
}
int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0;
for ( int i = 0; i < m; i++)
{
int value = x - arr1[i];
if (isPresent(arr2, 0, n - 1, value))
count++;
}
return count;
}
int main()
{
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 3, 5, 8};
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
int x = 10;
cout << "Count = "
<< countPairs(arr1, arr2, m, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isPresent( int arr[], int low,
int high, int value)
{
while (low <= high)
{
int mid = (low + high) / 2 ;
if (arr[mid] == value)
return true ;
else if (arr[mid] > value)
high = mid - 1 ;
else
low = mid + 1 ;
}
return false ;
}
static int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0 ;
for ( int i = 0 ; i < m; i++)
{
int value = x - arr1[i];
if (isPresent(arr2, 0 , n - 1 , value))
count++;
}
return count;
}
public static void main (String[] args)
{
int arr1[] = { 1 , 3 , 5 , 7 };
int arr2[] = { 2 , 3 , 5 , 8 };
int m = arr1.length;
int n = arr2.length;
int x = 10 ;
System.out.println( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
Python 3
def isPresent(arr, low, high, value):
while (low < = high):
mid = (low + high) / / 2
if (arr[mid] = = value):
return True
elif (arr[mid] > value) :
high = mid - 1
else :
low = mid + 1
return False
def countPairs(arr1, arr2, m, n, x):
count = 0
for i in range (m):
value = x - arr1[i]
if (isPresent(arr2, 0 , n - 1 , value)):
count + = 1
return count
if __name__ = = "__main__" :
arr1 = [ 1 , 3 , 5 , 7 ]
arr2 = [ 2 , 3 , 5 , 8 ]
m = len (arr1)
n = len (arr2)
x = 10
print ( "Count = " ,
countPairs(arr1, arr2, m, n, x))
|
C#
using System;
class GFG {
static bool isPresent( int []arr, int low,
int high, int value)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == value)
return true ;
else if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return false ;
}
static int countPairs( int []arr1, int []arr2,
int m, int n, int x)
{
int count = 0;
for ( int i = 0; i < m; i++)
{
int value = x - arr1[i];
if (isPresent(arr2, 0, n - 1, value))
count++;
}
return count;
}
public static void Main ()
{
int []arr1 = {1, 3, 5, 7};
int []arr2 = {2, 3, 5, 8};
int m = arr1.Length;
int n = arr2.Length;
int x = 10;
Console.WriteLine( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
PHP
<?php
function isPresent( $arr , $low ,
$high , $value )
{
while ( $low <= $high )
{
$mid = ( $low + $high ) / 2;
if ( $arr [ $mid ] == $value )
return true;
else if ( $arr [ $mid ] > $value )
$high = $mid - 1;
else
$low = $mid + 1;
}
return false;
}
function countPairs( $arr1 , $arr2 ,
$m , $n , $x )
{
$count = 0;
for ( $i = 0; $i < $m ; $i ++)
{
$value = $x - $arr1 [ $i ];
if (isPresent( $arr2 , 0,
$n - 1, $value ))
$count ++;
}
return $count ;
}
$arr1 = array (1, 3, 5, 7);
$arr2 = array (2, 3, 5, 8);
$m = count ( $arr1 );
$n = count ( $arr2 );
$x = 10;
echo "Count = "
, countPairs( $arr1 , $arr2 , $m , $n , $x );
?>
|
Javascript
<script>
function isPresent(arr,low,high,value)
{
while (low <= high)
{
let mid = Math.floor((low + high) / 2);
if (arr[mid] == value)
return true ;
else if (arr[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return false ;
}
function countPairs(arr1,arr2,m,n,x)
{
let count = 0;
for (let i = 0; i < m; i++)
{
let value = x - arr1[i];
if (isPresent(arr2, 0, n - 1, value))
count++;
}
return count;
}
let arr1=[1, 3, 5, 7];
let arr2=[2, 3, 5, 8];
let m=arr1.length;
let n = arr2.length;
let x = 10;
document.write( "Count = "
+ countPairs(arr1, arr2, m, n, x));
</script>
|
Output :
Count = 2
Time Complexity : O(mlogn), searching should be applied on the array which is of greater size so as to reduce the time complexity.
Auxiliary space : O(1)
Method 3 (Hashing): Hash table is implemented using unordered_set in C++. We store all first array elements in hash table. For elements of second array, we subtract every element from x and check the result in hash table. If result is present, we increment the count.
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0;
unordered_set< int > us;
for ( int i = 0; i < m; i++)
us.insert(arr1[i]);
for ( int j = 0; j < n; j++)
if (us.find(x - arr2[j]) != us.end())
count++;
return count;
}
int main()
{
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 3, 5, 8};
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
int x = 10;
cout << "Count = "
<< countPairs(arr1, arr2, m, n, x);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0 ;
HashSet<Integer> us = new HashSet<Integer>();
for ( int i = 0 ; i < m; i++)
us.add(arr1[i]);
for ( int j = 0 ; j < n; j++)
if (us.contains(x - arr2[j]))
count++;
return count;
}
public static void main(String[] args)
{
int arr1[] = { 1 , 3 , 5 , 7 };
int arr2[] = { 2 , 3 , 5 , 8 };
int m = arr1.length;
int n = arr2.length;
int x = 10 ;
System.out.print( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
Python3
def countPairs(arr1, arr2, m, n, x):
count = 0
us = set ()
for i in range (m):
us.add(arr1[i])
for j in range (n):
if x - arr2[j] in us:
count + = 1
return count
arr1 = [ 1 , 3 , 5 , 7 ]
arr2 = [ 2 , 3 , 5 , 8 ]
m = len (arr1)
n = len (arr2)
x = 10
print ( "Count =" ,
countPairs(arr1, arr2, m, n, x))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs( int []arr1, int []arr2,
int m, int n, int x)
{
int count = 0;
HashSet< int > us = new HashSet< int >();
for ( int i = 0; i < m; i++)
us.Add(arr1[i]);
for ( int j = 0; j < n; j++)
if (us.Contains(x - arr2[j]))
count++;
return count;
}
public static void Main(String[] args)
{
int []arr1 = {1, 3, 5, 7};
int []arr2 = {2, 3, 5, 8};
int m = arr1.Length;
int n = arr2.Length;
int x = 10;
Console.Write( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
Javascript
<script>
function countPairs(arr1, arr2, m, n, x)
{
let count = 0;
let us = new Set();
for (let i = 0; i < m; i++)
us.add(arr1[i]);
for (let j = 0; j < n; j++)
if (us.has(x - arr2[j]))
count++;
return count;
}
let arr1=[1, 3, 5, 7];
let arr2=[2, 3, 5, 8];
let m = arr1.length;
let n = arr2.length;
let x = 10;
document.write( "Count = "
+ countPairs(arr1, arr2, m, n, x));
</script>
|
Output :
Count = 2
Time Complexity : O(m+n)
Auxiliary space : O(m), hash table should be created of the array having smaller size so as to reduce the space complexity.
Method 4 (Efficient Approach): This approach uses the concept of two pointers, one to traverse 1st array from left to right and another to traverse the 2nd array from right to left.
Algorithm :
countPairs(arr1, arr2, m, n, x)
Initialize l = 0, r = n - 1
Initialize count = 0
loop while l = 0
if (arr1[l] + arr2[r]) == x
l++, r--
count++
else if (arr1[l] + arr2[r]) < x
l++
else
r--
return count
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr1[], int arr2[],
int m, int n, int x)
{
int count = 0;
int l = 0, r = n - 1;
while (l < m && r >= 0)
{
if ((arr1[l] + arr2[r]) == x)
{
l++; r--;
count++;
}
else if ((arr1[l] + arr2[r]) < x)
l++;
else
r--;
}
return count;
}
int main()
{
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 3, 5, 8};
int m = sizeof (arr1) / sizeof (arr1[0]);
int n = sizeof (arr2) / sizeof (arr2[0]);
int x = 10;
cout << "Count = "
<< countPairs(arr1, arr2, m, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPairs( int arr1[],
int arr2[], int m, int n, int x)
{
int count = 0 ;
int l = 0 , r = n - 1 ;
while (l < m && r >= 0 )
{
if ((arr1[l] + arr2[r]) == x)
{
l++; r--;
count++;
}
else if ((arr1[l] + arr2[r]) < x)
l++;
else
r--;
}
return count;
}
public static void main (String[] args)
{
int arr1[] = { 1 , 3 , 5 , 7 };
int arr2[] = { 2 , 3 , 5 , 8 };
int m = arr1.length;
int n = arr2.length;
int x = 10 ;
System.out.println( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
Python3
def countPairs(arr1, arr2, m, n, x):
count, l, r = 0 , 0 , n - 1
while (l < m and r > = 0 ):
if ((arr1[l] + arr2[r]) = = x):
l + = 1
r - = 1
count + = 1
elif ((arr1[l] + arr2[r]) < x):
l + = 1
else :
r - = 1
return count
if __name__ = = '__main__' :
arr1 = [ 1 , 3 , 5 , 7 ]
arr2 = [ 2 , 3 , 5 , 8 ]
m = len (arr1)
n = len (arr2)
x = 10
print ( "Count =" ,
countPairs(arr1, arr2,
m, n, x))
|
C#
using System;
class GFG {
static int countPairs( int []arr1,
int []arr2, int m, int n, int x)
{
int count = 0;
int l = 0, r = n - 1;
while (l < m && r >= 0)
{
if ((arr1[l] + arr2[r]) == x)
{
l++; r--;
count++;
}
else if ((arr1[l] + arr2[r]) < x)
l++;
else
r--;
}
return count;
}
public static void Main ()
{
int []arr1 = {1, 3, 5, 7};
int []arr2 = {2, 3, 5, 8};
int m = arr1.Length;
int n = arr2.Length;
int x = 10;
Console.WriteLine( "Count = "
+ countPairs(arr1, arr2, m, n, x));
}
}
|
PHP
<?php
function countPairs( $arr1 , $arr2 ,
$m , $n , $x )
{
$count = 0;
$l = 0; $r = $n - 1;
while ( $l < $m and $r >= 0)
{
if (( $arr1 [ $l ] + $arr2 [ $r ]) == $x )
{
$l ++; $r --;
$count ++;
}
else if (( $arr1 [ $l ] + $arr2 [ $r ]) < $x )
$l ++;
else
$r --;
}
return $count ;
}
$arr1 = array (1, 3, 5, 7);
$arr2 = array (2, 3, 5, 8);
$m = count ( $arr1 );
$n = count ( $arr2 );
$x = 10;
echo "Count = "
, countPairs( $arr1 , $arr2 , $m , $n , $x );
?>
|
Javascript
<script>
function countPairs(arr1, arr2, m, n, x)
{
let count = 0;
let l = 0, r = n - 1;
while (l < m && r >= 0)
{
if ((arr1[l] + arr2[r]) == x)
{
l++; r--;
count++;
}
else if ((arr1[l] + arr2[r]) < x)
l++;
else
r--;
}
return count;
}
let arr1 = [1, 3, 5, 7];
let arr2 = [2, 3, 5, 8];
let m = arr1.length;
let n = arr2.length;
let x = 10;
document.write( "Count = " + countPairs(arr1, arr2, m, n, x));
</script>
|
Output :
Count = 2
Time Complexity : O(m + n)
Auxiliary space : O(1)
Share your thoughts in the comments
Please Login to comment...