# Count pairs from two arrays having sum equal to K

Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with sum equal to K.
Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can participate only in one pair. For example, A1[] = {3, 3}, A2[] = {4, 4} and K = 7, we consider only two pairs (3, 4) and (3, 4)

Examples:

Input: A1[] = {1, 1, 3, 4, 5, 6, 6}, A2[] = {1, 4, 4, 5, 7}, K = 10
Output: 4
All possible pairs are {3, 7}, {4, 6}, {5, 5} and {4, 6}

Input: A1[] = {1, 10, 13, 15}, A2[] = {3, 3, 12, 4}, K = 13
Output: 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Create a map of the elements of array A1.
• For each element in array A2, check if temp = K – A2[i] exists in map created in previous step.
• If map[temp] > 0 then increment result by 1 and decrement map[temp] by 1.
• Print the total count in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach. ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of pairs  ` `// having sum equal to K ` `int` `countPairs(``int` `A1[], ``int` `A2[] ` `                  ``, ``int` `n1, ``int` `n2, ``int` `K) ` `{ ` `    ``// Initialize pairs to 0 ` `    ``int` `res = 0; ` ` `  `    ``// create map of elements of array A1 ` `    ``unordered_map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n1; ++i) ` `        ``m[A1[i]]++; ` ` `  `    ``// count total pairs ` `    ``for` `(``int` `i = 0; i < n2; ++i) { ` `        ``int` `temp = K - A2[i]; ` ` `  `        ``if` `(m[temp] != 0) { ` `            ``res++; ` ` `  `            ``// Every element can be part  ` `            ``// of at most one pair.  ` `            ``m[temp]--; ` `        ``} ` `    ``} ` ` `  `    ``// return total pairs ` `    ``return` `res; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `A1[] = { 1, 1, 3, 4, 5, 6, 6 }; ` `    ``int` `A2[] = { 1, 4, 4, 5, 7 }, K = 10; ` ` `  `    ``int` `n1 = ``sizeof``(A1) / ``sizeof``(A1); ` `    ``int` `n2 = ``sizeof``(A2) / ``sizeof``(A2); ` ` `  `    ``// function call to print required answer ` `    ``cout << countPairs(A1, A2, n1, n2, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach.  ` `import` `java.util.*; ` `class` `GfG { ` ` `  `// Function to return the count of pairs  ` `// having sum equal to K  ` `static` `int` `countPairs(``int` `A1[], ``int` `A2[] , ``int` `n1, ``int` `n2, ``int` `K)  ` `{  ` `    ``// Initialize pairs to 0  ` `    ``int` `res = ``0``;  ` ` `  `    ``// create map of elements of array A1  ` `    ``Map m = ``new` `HashMap ();  ` `    ``for` `(``int` `i = ``0``; i < n1; ++i)  ` `    ``{ ` `        ``if``(m.containsKey(A1[i])) ` `        ``m.put(A1[i], m.get(A1[i]) + ``1``); ` `        ``else` `        ``m.put(A1[i], ``1``); ` `    ``}  ` ` `  `    ``// count total pairs  ` `    ``for` `(``int` `i = ``0``; i < n2; ++i) {  ` `        ``int` `temp = K - A2[i];  ` ` `  `        ``if` `(m.containsKey(temp) && m.get(temp) != ``0``) {  ` `            ``res++;  ` ` `  `            ``// Every element can be part  ` `            ``// of at most one pair.  ` `            ``m.put(temp, m.get(A1[i]) - ``1``); ` `        ``}  ` `    ``}  ` ` `  `    ``// return total pairs  ` `    ``return` `res;  ` `}  ` ` `  `// Driver program  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `A1[] = { ``1``, ``1``, ``3``, ``4``, ``5``, ``6``, ``6` `};  ` `    ``int` `A2[] = { ``1``, ``4``, ``4``, ``5``, ``7` `}, K = ``10``;  ` ` `  `    ``int` `n1 = A1.length; ` `    ``int` `n2 = A2.length;  ` ` `  `    ``// function call to print required answer  ` `    ``System.out.println(countPairs(A1, A2, n1, n2, K));  ` `}  ` `} `

## Python3

 `# Python3 implementation of above approach ` ` `  `# Function to return the count of  ` `# pairs having sum equal to K ` `def` `countPairs(A1, A2, n1, n2, K): ` `     `  `    ``# Initialize pairs to 0 ` `    ``res ``=` `0` `     `  `    ``# Create dictionary of elements ` `    ``# of array A1 ` `    ``m ``=` `dict``() ` `    ``for` `i ``in` `range``(``0``, n1): ` `        ``if` `A1[i] ``not` `in` `m.keys(): ` `            ``m[A1[i]] ``=` `1` `        ``else``: ` `            ``m[A1[i]] ``=` `m[A1[i]] ``+` `1` `         `  `    ``# count total pairs ` `    ``for` `i ``in` `range``(``0``, n2): ` `        ``temp ``=` `K ``-` `A2[i] ` `        ``if` `temp ``in` `m.keys(): ` `            ``res ``=` `res ``+` `1` `             `  `            ``# Every element can be part ` `            ``# of at most one pair ` `            ``m[temp] ``=` `m[temp] ``-` `1` `     `  `    ``# return total pairs ` `    ``return` `res ` ` `  `# Driver Code ` `A1 ``=` `[``1``, ``1``, ``3``, ``4``, ``5``, ``6` `,``6``] ` `A2 ``=` `[``1``, ``4``, ``4``, ``5``, ``7``] ` `K ``=` `10` ` `  `n1 ``=` `len``(A1) ` `n2 ``=` `len``(A2) ` ` `  `# function call to print required answer ` `print``(countPairs(A1, A2, n1, n2, K)) ` `         `  `# This code is contributed  ` `# by Shashank_Sharma `

## C#

 `// C# implementation of above approach.  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GfG ` `{ ` ` `  `// Function to return the count of pairs  ` `// having sum equal to K  ` `static` `int` `countPairs(``int` `[]A1, ``int` `[]A2 , ` `                        ``int` `n1, ``int` `n2, ``int` `K)  ` `{  ` `    ``// Initialize pairs to 0  ` `    ``int` `res = 0;  ` ` `  `    ``// create map of elements of array A1  ` `    ``Dictionary<``int``,``int``> m = ``new` `Dictionary<``int``,``int``> ();  ` `    ``for` `(``int` `i = 0; i < n1; ++i)  ` `    ``{ ` `        ``int` `a; ` `        ``if``(m.ContainsKey(A1[i])) ` `        ``{ ` `            ``a = m[A1[i]] + 1; ` `            ``m.Remove(A1[i]); ` `            ``m.Add(A1[i], a); ` `        ``} ` `        ``else` `        ``m.Add(A1[i], 1); ` `    ``}  ` ` `  `    ``// count total pairs  ` `    ``for` `(``int` `i = 0; i < n2; ++i) ` `    ``{  ` `        ``int` `temp = K - A2[i];  ` ` `  `        ``if` `(m.ContainsKey(temp) && m[temp] != 0) ` `        ``{  ` `            ``res++;  ` ` `  `            ``// Every element can be part  ` `            ``// of at most one pair.  ` `            ``m.Remove(temp); ` `            ``m.Add(temp, m[A1[i]] - 1); ` `        ``}  ` `    ``}  ` ` `  `    ``// return total pairs  ` `    ``return` `res;  ` `}  ` ` `  `// Driver program  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `[]A1 = { 1, 1, 3, 4, 5, 6, 6 };  ` `    ``int` `[]A2 = { 1, 4, 4, 5, 7 }; ` `    ``int` `K = 10;  ` ` `  `    ``int` `n1 = A1.Length; ` `    ``int` `n2 = A2.Length;  ` ` `  `    ``// function call to print required answer  ` `    ``Console.WriteLine(countPairs(A1, A2, n1, n2, K));  ` `}  ` `}  ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```4
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.