# Print all pairs in an unsorted array with equal sum

Given an unsorted array A[]. The task is to print all unique pairs in the unsorted array with equal sum.

Note: Print the result in the format as shown in the below examples.

Examples:

```Input: A[] = { 6, 4, 12, 10, 22, 54, 32, 42, 21, 11}
Output:
Pairs : ( 4, 12) ( 6, 10)  have sum : 16
Pairs : ( 10, 22) ( 21, 11)  have sum : 32
Pairs : ( 12, 21) ( 22, 11)  have sum : 33
Pairs : ( 22, 21) ( 32, 11)  have sum : 43
Pairs : ( 32, 21) ( 42, 11)  have sum : 53
Pairs : ( 12, 42) ( 22, 32)  have sum : 54
Pairs : ( 10, 54) ( 22, 42)  have sum : 64

Input:A[]= { 4, 23, 65, 67, 24, 12, 86}
Output:
Pairs : ( 4, 86) ( 23, 67) have sum : 90
```

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

The idea is to use map in C++ STL for avoiding duplicate pair of elements.

• Create a map with key as pair of integer and value as integer to store all unique pair of elements and their corresponding sum.
• Traverse the array and generate all possible pairs and store the pairs and their corresponding sum in first map.
• Create a second map with key as integer and value as a vector of pair to store list of all pair of elements with a corresponding sum.
• Finally, traverse the second map, and for a sum with more than one pair, print all pairs and then the corresponding sum in a format as shown in the above example.

Below is the implementation of the above approach:

## C++

 `// C++ program to print all pairs ` `// with equal sum ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print all pairs ` `// with equal sum ` `void` `pairWithEqualSum(``int` `A[], ``int` `n) ` `{ ` ` `  `    ``map<``int``, vector > > mp; ` ` `  `    ``// Insert all unique pairs and their ` `    ``// corresponding sum in the map ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` `        ``for` `(``int` `j = i + 1; j < n; j++) { ` `            ``pair<``int``, ``int``> p = make_pair(A[i], A[j]); ` ` `  `            ``mp[A[i] + A[j]].push_back(p); ` `        ``} ` `    ``} ` ` `  `    ``// Traverse the map mp, and for sum ` `    ``// with more than one pair, print all pairs ` `    ``// and the corresponding sum ` `    ``for` `(``auto` `itr = mp.begin(); itr != mp.end(); itr++) { ` `        ``if` `(itr->second.size() > 1) { ` `            ``cout << ``"Pairs : "``; ` ` `  `            ``for` `(``int` `i = 0; i < itr->second.size(); i++) { ` `                ``cout << ``"( "` `<< itr->second[i].first << ``", "` `                     ``<< itr->second[i].second << ``") "``; ` `            ``} ` ` `  `            ``cout << ``" have sum : "` `<< itr->first << endl; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A[] = { 6, 4, 12, 10, 22, 54, 32, 42, 21, 11, 8, 2 }; ` ` `  `    ``int` `n = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``pairWithEqualSum(A, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print all pairs ` `// with equal sum ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `    ``static` `class` `pair ` `    ``{  ` `        ``int` `first, second;  ` `        ``public` `pair(``int` `first, ``int` `second)   ` `        ``{  ` `            ``this``.first = first;  ` `            ``this``.second = second;  ` `        ``}     ` `}  ` ` `  `// Function to print all pairs ` `// with equal sum ` `static` `void` `pairWithEqualSum(``int` `A[], ``int` `n) ` `{ ` `  `  `    ``Map > mp = ``new` `HashMap<>(); ` `  `  `    ``// Insert all unique pairs and their ` `    ``// corresponding sum in the map ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) { ` `        ``for` `(``int` `j = i + ``1``; j < n; j++) { ` `            ``pair p = ``new` `pair(A[i], A[j]); ` `            ``Vector pp = ``new` `Vector(); ` `            ``if``(mp.containsKey(A[i] + A[j])) ` `                ``pp.addAll(mp.get(A[i] + A[j])); ` `            ``pp.add(p); ` `            ``mp.put(A[i] + A[j],pp); ` `        ``} ` `    ``} ` `  `  `    ``// Traverse the map mp, and for sum ` `    ``// with more than one pair, print all pairs ` `    ``// and the corresponding sum ` `    ``for` `(Map.Entry> itr : mp.entrySet()){ ` `        ``if` `(itr.getValue().size() > ``1``) { ` `            ``System.out.print(``"Pairs : "``); ` `  `  `            ``for` `(``int` `i = ``0``; i < itr.getValue().size(); i++) { ` `                ``System.out.print(``"( "` `+  itr.getValue().get(i).first+ ``", "` `                     ``+ itr.getValue().get(i).second+ ``") "``); ` `            ``} ` `  `  `            ``System.out.print(``" have sum : "` `+  itr.getKey() +``"\n"``); ` `        ``} ` `    ``} ` `} ` `  `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `A[] = { ``6``, ``4``, ``12``, ``10``, ``22``, ``54``, ``32``, ``42``, ``21``, ``11``, ``8``, ``2` `}; ` `  `  `    ``int` `n = A.length; ` `  `  `    ``pairWithEqualSum(A, n);  ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python implementation of the ` `# approach ` ` `  `# Function to print all pairs ` `# with equal sum ` `def` `pairWithEqualSum(A, n):  ` `    ``mp ``=` `{}  ` ` `  `    ``# Insert all unique pairs and their ` `    ``# corresponding sum in the map ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``for` `j ``in` `range``(i``+``1``,n): ` `            ``if` `A[i]``+``A[j] ``in` `mp: ` `                ``mp[A[i]``+``A[j]].append((A[i], A[j])) ` `            ``else``: ` `                ``mp[A[i]``+``A[j]] ``=` `[ (A[i],A[j]) ] ` ` `  ` `  `    ``# Traverse the map mp, and for sum ` `    ``# with more than one pair, print all pairs ` `    ``# and the corresponding sum ` `     `  `    ``for` `itr ``in` `mp:  ` `        ``if` `len``(mp[itr]) > ``1``:  ` `            ``print``(``"Pairs : "``, end ``=` `"")  ` `            ``for` `i ``in` `range``(``0``, ``len``(mp[itr])):  ` `                ``print``(``"("``, mp[itr][i][``0``], ``","``,  ` `                    ``mp[itr][i][``1``], ``")"``, end ``=` `" "``)  ` `             `  `            ``print``(``"have sum :"``, itr)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``A ``=` `[``6``, ``4``, ``12``, ``10``, ``22``, ``54``,  ` `        ``32``, ``42``, ``21``, ``11``, ``8``, ``2``]  ` `    ``n ``=` `len``(A)  ` ` `  `    ``pairWithEqualSum(A, n) `

## C#

 `// C# program to print all pairs ` `// with equal sum ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `    ``class` `pair ` `    ``{  ` `        ``public` `int` `first, second;  ` `        ``public` `pair(``int` `first, ``int` `second)   ` `        ``{  ` `            ``this``.first = first;  ` `            ``this``.second = second;  ` `        ``}     ` `}  ` `  `  `// Function to print all pairs ` `// with equal sum ` `static` `void` `pairWithEqualSum(``int` `[]A, ``int` `n) ` `{ ` `   `  `    ``Dictionary<``int``, List> mp = ``new` `Dictionary<``int``, List>(); ` `   `  `    ``// Insert all unique pairs and their ` `    ``// corresponding sum in the map ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` `        ``for` `(``int` `j = i + 1; j < n; j++) { ` `            ``pair p = ``new` `pair(A[i], A[j]); ` `            ``List pp = ``new` `List(); ` `            ``if``(mp.ContainsKey(A[i] + A[j])) ` `                ``pp = AddAll(mp[A[i] + A[j]]); ` `            ``pp.Add(p); ` `            ``if``(mp.ContainsKey(A[i] + A[j])) ` `                ``mp[A[i] + A[j]] = pp; ` `            ``else` `                ``mp.Add(A[i] + A[j],pp);  ` `        ``} ` `    ``} ` ` `  `    ``// Traverse the map mp, and for sum ` `    ``// with more than one pair, print all pairs ` `    ``// and the corresponding sum ` `    ``foreach``(KeyValuePair<``int``,List> itr ``in` `mp){ ` `        ``if` `(itr.Value.Count > 1) { ` `            ``Console.Write(``"Pairs : "``); ` `   `  `            ``for` `(``int` `i = 0; i < itr.Value.Count; i++) { ` `                ``Console.Write(``"( "` `+  itr.Value[i].first+ ``", "` `                     ``+ itr.Value[i].second+ ``") "``); ` `            ``} ` `   `  `            ``Console.Write(``" have sum : "` `+  itr.Key +``"\n"``); ` `        ``} ` `    ``} ` `} ` `       ``static` `List AddAll(List list){ ` `        ``List l = ``new` `List(); ` `        ``foreach` `(pair p ``in` `list) ` `            ``l.Add(p); ` `        ``return` `l; ` `    ``}  ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]A = { 6, 4, 12, 10, 22, 54, 32, 42, 21, 11, 8, 2 }; ` `   `  `    ``int` `n = A.Length; ` `   `  `    ``pairWithEqualSum(A, n);  ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Pairs : ( 6, 4) ( 8, 2)  have sum : 10
Pairs : ( 4, 8) ( 10, 2)  have sum : 12
Pairs : ( 6, 8) ( 4, 10) ( 12, 2)  have sum : 14
Pairs : ( 6, 10) ( 4, 12)  have sum : 16
Pairs : ( 6, 12) ( 10, 8)  have sum : 18
Pairs : ( 12, 11) ( 21, 2)  have sum : 23
Pairs : ( 10, 22) ( 21, 11)  have sum : 32
Pairs : ( 12, 21) ( 22, 11)  have sum : 33
Pairs : ( 12, 22) ( 32, 2)  have sum : 34
Pairs : ( 22, 21) ( 32, 11)  have sum : 43
Pairs : ( 12, 32) ( 42, 2)  have sum : 44
Pairs : ( 32, 21) ( 42, 11)  have sum : 53
Pairs : ( 12, 42) ( 22, 32)  have sum : 54
Pairs : ( 10, 54) ( 22, 42)  have sum : 64
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.