# 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```

Recommended Practice

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++

 `// C++ implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `#include ` `using` `namespace` `std; ` ` `  `// function to count all pairs ` `// from both the sorted arrays  ` `// whose sum is equal to a given ` `// value ` `int` `countPairs(``int` `arr1[], ``int` `arr2[],  ` `               ``int` `m, ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = 0; ` `     `  `    ``// generating pairs from  ` `    ``// both the arrays ` `    ``for` `(``int` `i = 0; i < m; i++) ` `        ``for` `(``int` `j = 0; j < n; j++) ` ` `  `            ``// if sum of pair is equal  ` `            ``// to 'x' increment count  ` `            ``if` `((arr1[i] + arr2[j]) == x)  ` `                ``count++; ` `     `  `    ``// required count of pairs      ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `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

 `// Java implementation to count pairs from ` `// both sorted arrays whose sum is equal ` `// to a given value ` `import` `java.io.*; ` ` `  `class` `GFG { ` `         `  `    ``// function to count all pairs ` `    ``// from both the sorted arrays  ` `    ``// whose sum is equal to a given ` `    ``// value ` `    ``static` `int` `countPairs(``int` `[]arr1,  ` `             ``int` `[]arr2, ``int` `m, ``int` `n, ``int` `x) ` `    ``{ ` `        ``int` `count = ``0``; ` `         `  `        ``// generating pairs from  ` `        ``// both the arrays ` `        ``for` `(``int` `i = ``0``; i < m; i++) ` `            ``for` `(``int` `j = ``0``; j < n; j++) ` `     `  `                ``// if sum of pair is equal  ` `                ``// to 'x' increment count  ` `                ``if` `((arr1[i] + arr2[j]) == x)  ` `                    ``count++; ` `         `  `        ``// required count of pairs  ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` ` `  `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# python implementation to count ` `# pairs from both sorted arrays  ` `# whose sum is equal to a given  ` `# value ` ` `  `# function to count all pairs from ` `# both the sorted arrays whose sum ` `# is equal to a given value ` `def` `countPairs(arr1, arr2, m, n, x): ` `    ``count ``=` `0` ` `  `    ``# generating pairs from both ` `    ``# the arrays ` `    ``for` `i ``in` `range``(m): ` `        ``for` `j ``in` `range``(n): ` ` `  `            ``# if sum of pair is equal ` `            ``# to 'x' increment count ` `            ``if` `arr1[i] ``+` `arr2[j] ``=``=` `x: ` `                ``count ``=` `count ``+` `1` ` `  `    ``# required count of pairs ` `    ``return` `count ` ` `  `# Driver Program ` `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)) ` ` `  `# This code is contributed by Shrikant13. `

## C#

 `// C# implementation to count pairs from ` `// both sorted arrays whose sum is equal ` `// to a given value ` `using` `System; ` ` `  `class` `GFG { ` `         `  `    ``// function to count all pairs ` `    ``// from both the sorted arrays  ` `    ``// whose sum is equal to a given ` `    ``// value ` `    ``static` `int` `countPairs(``int` `[]arr1,  ` `            ``int` `[]arr2, ``int` `m, ``int` `n, ``int` `x) ` `    ``{ ` `        ``int` `count = 0; ` `         `  `        ``// generating pairs from  ` `        ``// both the arrays ` `        ``for` `(``int` `i = 0; i < m; i++) ` `            ``for` `(``int` `j = 0; j < n; j++) ` `     `  `                ``// if sum of pair is equal  ` `                ``// to 'x' increment count  ` `                ``if` `((arr1[i] + arr2[j]) == x)  ` `                    ``count++; ` `         `  `        ``// required count of pairs  ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` ` `  `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

## Javascript

 ``

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++

 `// C++ implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given ` `// value ` `#include ` `using` `namespace` `std; ` ` `  `// function to search 'value'  ` `// in the given array 'arr[]'  ` `// it uses binary search technique  ` `// as  'arr[]' is sorted  ` `bool` `isPresent(``int` `arr[], ``int` `low, ` `               ``int` `high, ``int` `value) ` `{ ` `    ``while` `(low <= high) ` `    ``{ ` `        ``int` `mid = (low + high) / 2; ` `         `  `        ``// value found ` `        ``if` `(arr[mid] == value) ` `            ``return` `true``;      ` `             `  `        ``else` `if` `(arr[mid] > value)  ` `            ``high = mid - 1; ` `        ``else` `            ``low = mid + 1;  ` `    ``} ` `     `  `    ``// value not found ` `    ``return` `false``; ` `} ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given ` `// value ` `int` `countPairs(``int` `arr1[], ``int` `arr2[], ` `               ``int` `m, ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = 0;      ` `    ``for` `(``int` `i = 0; i < m; i++) ` `    ``{ ` `        ``// for each arr1[i] ` `        ``int` `value = x - arr1[i]; ` `         `  `        ``// check if the 'value' ` `        ``// is present in 'arr2[]' ` `        ``if` `(isPresent(arr2, 0, n - 1, value)) ` `            ``count++; ` `    ``} ` `     `  `    ``// required count of pairs      ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `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

 `// Java implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given ` `// value ` `import` `java.io.*; ` `class` `GFG { ` ` `  `// function to search 'value'  ` `// in the given array 'arr[]'  ` `// it uses binary search technique  ` `// as 'arr[]' is sorted  ` `static` `boolean` `isPresent(``int` `arr[], ``int` `low, ` `                         ``int` `high, ``int` `value) ` `{ ` `    ``while` `(low <= high) ` `    ``{ ` `        ``int` `mid = (low + high) / ``2``; ` `         `  `        ``// value found ` `        ``if` `(arr[mid] == value) ` `            ``return` `true``;      ` `             `  `        ``else` `if` `(arr[mid] > value)  ` `            ``high = mid - ``1``; ` `        ``else` `            ``low = mid + ``1``;  ` `    ``} ` `     `  `    ``// value not found ` `    ``return` `false``; ` `} ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given ` `// value ` `static` `int` `countPairs(``int` `arr1[], ``int` `arr2[], ` `                      ``int` `m, ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = ``0``;  ` `    ``for` `(``int` `i = ``0``; i < m; i++) ` `    ``{ ` `         `  `        ``// for each arr1[i] ` `        ``int` `value = x - arr1[i]; ` `         `  `        ``// check if the 'value' ` `        ``// is present in 'arr2[]' ` `        ``if` `(isPresent(arr2, ``0``, n - ``1``, value)) ` `            ``count++; ` `    ``} ` `     `  `    ``// required count of pairs  ` `    ``return` `count; ` `} ` ` `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python 3

 `# Python 3 implementation to count  ` `# pairs from both sorted arrays  ` `# whose sum is equal to a given ` `# value ` ` `  `# function to search 'value'  ` `# in the given array 'arr[]'  ` `# it uses binary search technique  ` `# as 'arr[]' is sorted  ` `def` `isPresent(arr, low, high, value): ` ` `  `    ``while` `(low <``=` `high): ` `     `  `        ``mid ``=` `(low ``+` `high) ``/``/` `2` `         `  `        ``# value found ` `        ``if` `(arr[mid] ``=``=` `value): ` `            ``return` `True` `             `  `        ``elif` `(arr[mid] > value) : ` `            ``high ``=` `mid ``-` `1` `        ``else``: ` `            ``low ``=` `mid ``+` `1` `     `  `    ``# value not found ` `    ``return` `False` ` `  `# function to count all pairs  ` `# from both the sorted arrays  ` `# whose sum is equal to a given ` `# value ` `def` `countPairs(arr1, arr2, m, n, x): ` `    ``count ``=` `0` `    ``for` `i ``in` `range``(m): ` `        ``# for each arr1[i] ` `        ``value ``=` `x ``-` `arr1[i] ` `         `  `        ``# check if the 'value' ` `        ``# is present in 'arr2[]' ` `        ``if` `(isPresent(arr2, ``0``, n ``-` `1``, value)): ` `            ``count ``+``=` `1` `     `  `    ``# required count of pairs      ` `    ``return` `count ` ` `  `# Driver Code ` `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)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# implementation to count pairs from both  ` `// sorted arrays whose sum is equal to a given ` `// value ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function to search 'value' in the given ` `    ``// array 'arr[]' it uses binary search  ` `    ``// technique as 'arr[]' is sorted  ` `    ``static` `bool` `isPresent(``int` `[]arr, ``int` `low, ` `                         ``int` `high, ``int` `value) ` `    ``{ ` `        ``while` `(low <= high) ` `        ``{ ` `            ``int` `mid = (low + high) / 2; ` `             `  `            ``// value found ` `            ``if` `(arr[mid] == value) ` `                ``return` `true``;      ` `                 `  `            ``else` `if` `(arr[mid] > value)  ` `                ``high = mid - 1; ` `            ``else` `                ``low = mid + 1;  ` `        ``} ` `         `  `        ``// value not found ` `        ``return` `false``; ` `    ``} ` `     `  `    ``// function to count all pairs  ` `    ``// from both the sorted arrays  ` `    ``// whose sum is equal to a given ` `    ``// value ` `    ``static` `int` `countPairs(``int` `[]arr1, ``int` `[]arr2, ` `                             ``int` `m, ``int` `n, ``int` `x) ` `    ``{ ` `        ``int` `count = 0;  ` `         `  `        ``for` `(``int` `i = 0; i < m; i++) ` `        ``{ ` `             `  `            ``// for each arr1[i] ` `            ``int` `value = x - arr1[i]; ` `             `  `            ``// check if the 'value' ` `            ``// is present in 'arr2[]' ` `            ``if` `(isPresent(arr2, 0, n - 1, value)) ` `                ``count++; ` `        ``} ` `         `  `        ``// required count of pairs  ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` ``\$value``)  ` `            ``\$high` `= ``\$mid` `- 1; ` `        ``else` `            ``\$low` `= ``\$mid` `+ 1;  ` `    ``} ` `     `  `    ``// value not found ` `    ``return` `false; ` `} ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given ` `// value ` `function` `countPairs(``\$arr1``, ``\$arr2``, ` `                    ``\$m``, ``\$n``, ``\$x``) ` `{ ` `    ``\$count` `= 0;  ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++) ` `    ``{ ` `         `  `        ``// for each arr1[i] ` `        ``\$value` `= ``\$x` `- ``\$arr1``[``\$i``]; ` `         `  `        ``// check if the 'value' ` `        ``// is present in 'arr2[]' ` `        ``if` `(isPresent(``\$arr2``, 0,  ` `                      ``\$n` `- 1, ``\$value``)) ` `            ``\$count``++; ` `    ``} ` `     `  `    ``// required count of pairs  ` `    ``return` `\$count``; ` `} ` ` `  `    ``// Driver Code ` `    ``\$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``); ` ` `  `// This code is contributed by anuj_67. ` `?> `

## Javascript

 ``

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++

 `// C++ implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `#include ` `using` `namespace` `std; ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given ` `// value ` `int` `countPairs(``int` `arr1[], ``int` `arr2[],  ` `               ``int` `m, ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = 0; ` `     `  `    ``unordered_set<``int``> us; ` `     `  `    ``// insert all the elements  ` `    ``// of 1st array in the hash ` `    ``// table(unordered_set 'us') ` `    ``for` `(``int` `i = 0; i < m; i++) ` `        ``us.insert(arr1[i]); ` `     `  `    ``// for each element of 'arr2[]  ` `    ``for` `(``int` `j = 0; j < n; j++)  ` ` `  `        ``// find (x - arr2[j]) in 'us' ` `        ``if` `(us.find(x - arr2[j]) != us.end()) ` `            ``count++; ` `     `  `    ``// required count of pairs      ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `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.*; ` `// Java implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` ` `  `class` `GFG ` `{ ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given  ` `// value  ` `static` `int` `countPairs(``int` `arr1[], ``int` `arr2[],  ` `            ``int` `m, ``int` `n, ``int` `x)  ` `{  ` `    ``int` `count = ``0``;  ` `     `  `    ``HashSet us = ``new` `HashSet(); ` `     `  `    ``// insert all the elements  ` `    ``// of 1st array in the hash  ` `    ``// table(unordered_set 'us')  ` `    ``for` `(``int` `i = ``0``; i < m; i++)  ` `        ``us.add(arr1[i]);  ` `     `  `    ``// for each element of 'arr2[]  ` `    ``for` `(``int` `j = ``0``; j < n; j++)  ` ` `  `        ``// find (x - arr2[j]) in 'us'  ` `        ``if` `(us.contains(x - arr2[j]))  ` `            ``count++;  ` `     `  `    ``// required count of pairs  ` `    ``return` `count;  ` `}  ` ` `  `// Driver Code  ` `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)); ` `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## Python3

 `# Python3 implementation to count  ` `# pairs from both sorted arrays  ` `# whose sum is equal to a given value  ` ` `  `# function to count all pairs from   ` `# both the sorted arrays whose sum ` `# is equal to a given value  ` `def` `countPairs(arr1, arr2, m, n, x): ` `    ``count ``=` `0` `    ``us ``=` `set``() ` ` `  `    ``# insert all the elements  ` `    ``# of 1st array in the hash  ` `    ``# table(unordered_set 'us')  ` `    ``for` `i ``in` `range``(m): ` `        ``us.add(arr1[i]) ` ` `  `    ``# or each element of 'arr2[]  ` `    ``for` `j ``in` `range``(n): ` ` `  `        ``# find (x - arr2[j]) in 'us'  ` `        ``if` `x ``-` `arr2[j] ``in` `us: ` `            ``count ``+``=` `1` ` `  `    ``# required count of pairs ` `    ``return` `count ` ` `  `# Driver code ` `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)) ` ` `  `# This code is contributed by Shrikant13 `

## C#

 `// C# implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given  ` `// value  ` `static` `int` `countPairs(``int` `[]arr1, ``int` `[]arr2,  ` `            ``int` `m, ``int` `n, ``int` `x)  ` `{  ` `    ``int` `count = 0;  ` `     `  `    ``HashSet<``int``> us = ``new` `HashSet<``int``>(); ` `     `  `    ``// insert all the elements  ` `    ``// of 1st array in the hash  ` `    ``// table(unordered_set 'us')  ` `    ``for` `(``int` `i = 0; i < m; i++)  ` `        ``us.Add(arr1[i]);  ` `     `  `    ``// for each element of 'arr2[]  ` `    ``for` `(``int` `j = 0; j < n; j++)  ` ` `  `        ``// find (x - arr2[j]) in 'us'  ` `        ``if``(us.Contains(x - arr2[j]))  ` `            ``count++;  ` `     `  `    ``// required count of pairs  ` `    ``return` `count;  ` `}  ` ` `  `// Driver Code  ` `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)); ` `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Javascript

 ``

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++

 `// C++ implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `#include ` `using` `namespace` `std; ` ` `  `// function to count all pairs  ` `// from both the sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `int` `countPairs(``int` `arr1[], ``int` `arr2[],  ` `               ``int` `m, ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = 0;  ` `    ``int` `l = 0, r = n - 1; ` `     `  `    ``// traverse 'arr1[]' from  ` `    ``// left to right ` `    ``// traverse 'arr2[]' from  ` `    ``// right to left ` `    ``while` `(l < m && r >= 0) ` `    ``{ ` `        ``// if this sum is equal  ` `        ``// to 'x', then increment 'l',  ` `        ``// decrement 'r' and ` `        ``// increment 'count' ` `        ``if` `((arr1[l] + arr2[r]) == x) ` `        ``{ ` `            ``l++; r--; ` `            ``count++;          ` `        ``} ` `         `  `        ``// if this sum is less  ` `        ``// than x, then increment l ` `        ``else` `if` `((arr1[l] + arr2[r]) < x) ` `            ``l++; ` `             `  `        ``// else decrement 'r'  ` `        ``else` `            ``r--;  ` `    ``} ` `     `  `    ``// required count of pairs      ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `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

 `// Java implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// function to count all pairs  ` `    ``// from both the sorted arrays  ` `    ``// whose sum is equal to a given  ` `    ``// value ` `    ``static` `int` `countPairs(``int` `arr1[],  ` `         ``int` `arr2[], ``int` `m, ``int` `n, ``int` `x) ` `    ``{ ` `        ``int` `count = ``0``;  ` `        ``int` `l = ``0``, r = n - ``1``; ` `         `  `        ``// traverse 'arr1[]' from  ` `        ``// left to right ` `        ``// traverse 'arr2[]' from  ` `        ``// right to left ` `        ``while` `(l < m && r >= ``0``) ` `        ``{ ` `             `  `            ``// if this sum is equal  ` `            ``// to 'x', then increment 'l',  ` `            ``// decrement 'r' and ` `            ``// increment 'count' ` `            ``if` `((arr1[l] + arr2[r]) == x) ` `            ``{ ` `                ``l++; r--; ` `                ``count++;          ` `            ``} ` `             `  `            ``// if this sum is less  ` `            ``// than x, then increment l ` `            ``else` `if` `((arr1[l] + arr2[r]) < x) ` `                ``l++; ` `                 `  `            ``// else decrement 'r'  ` `            ``else` `                ``r--;  ` `        ``} ` `         `  `        ``// required count of pairs  ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Python 3 implementation to count ` `# pairs from both sorted arrays ` `# whose sum is equal to a given ` `# value ` ` `  `# function to count all pairs ` `# from both the sorted arrays ` `# whose sum is equal to a given ` `# value ` `def` `countPairs(arr1, arr2, m, n, x): ` `    ``count, l, r ``=` `0``, ``0``, n ``-` `1` `     `  `    ``# traverse 'arr1[]' from ` `    ``# left to right ` `    ``# traverse 'arr2[]' from ` `    ``# right to left ` `    ``while` `(l < m ``and` `r >``=` `0``): ` `         `  `        ``# if this sum is equal ` `        ``# to 'x', then increment 'l', ` `        ``# decrement 'r' and ` `        ``# increment 'count' ` `        ``if` `((arr1[l] ``+` `arr2[r]) ``=``=` `x): ` `            ``l ``+``=` `1` `            ``r ``-``=` `1` `            ``count ``+``=` `1` `             `  `        ``# if this sum is less ` `        ``# than x, then increment l ` `        ``elif` `((arr1[l] ``+` `arr2[r]) < x): ` `            ``l ``+``=` `1` `             `  `        ``# else decrement 'r' ` `        ``else``: ` `            ``r ``-``=` `1` `             `  `    ``# required count of pairs ` `    ``return` `count ` ` `  `# Driver Code ` `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)) ` ` `  `# This code is contributed  ` `# by PrinciRaj19992 `

## C#

 `// C# implementation to count  ` `// pairs from both sorted arrays  ` `// whose sum is equal to a given  ` `// value ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function to count all pairs  ` `    ``// from both the sorted arrays  ` `    ``// whose sum is equal to a given  ` `    ``// value ` `    ``static` `int` `countPairs(``int` `[]arr1,  ` `        ``int` `[]arr2, ``int` `m, ``int` `n, ``int` `x) ` `    ``{ ` `        ``int` `count = 0;  ` `        ``int` `l = 0, r = n - 1; ` `         `  `        ``// traverse 'arr1[]' from  ` `        ``// left to right ` `        ``// traverse 'arr2[]' from  ` `        ``// right to left ` `        ``while` `(l < m && r >= 0) ` `        ``{ ` `             `  `            ``// if this sum is equal  ` `            ``// to 'x', then increment 'l',  ` `            ``// decrement 'r' and ` `            ``// increment 'count' ` `            ``if` `((arr1[l] + arr2[r]) == x) ` `            ``{ ` `                ``l++; r--; ` `                ``count++;          ` `            ``} ` `             `  `            ``// if this sum is less  ` `            ``// than x, then increment l ` `            ``else` `if` `((arr1[l] + arr2[r]) < x) ` `                ``l++; ` `                 `  `            ``// else decrement 'r'  ` `            ``else` `                ``r--;  ` `        ``} ` `         `  `        ``// required count of pairs  ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 `= 0) ` `    ``{ ` `        ``// if this sum is equal  ` `        ``// to 'x', then increment 'l',  ` `        ``// decrement 'r' and ` `        ``// increment 'count' ` `        ``if` `((``\$arr1``[``\$l``] + ``\$arr2``[``\$r``]) == ``\$x``) ` `        ``{ ` `            ``\$l``++; ``\$r``--; ` `            ``\$count``++;          ` `        ``} ` `         `  `        ``// if this sum is less  ` `        ``// than x, then increment l ` `        ``else` `if` `((``\$arr1``[``\$l``] + ``\$arr2``[``\$r``]) < ``\$x``) ` `            ``\$l``++; ` `             `  `        ``// else decrement 'r'  ` `        ``else` `            ``\$r``--;  ` `    ``} ` `     `  `    ``// required count of pairs      ` `    ``return` `\$count``; ` `} ` ` `  `// Driver Code ` `     ``\$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``); ` `// This code is contributed by anuj_67 ` ` `  `?> `

## Javascript

 ``

Output :

`Count = 2`

Time Complexity : O(m + n)
Auxiliary space : O(1)

Previous
Next