# Print all pairs with given sum

Given an array of integers, and a number ‘sum’, print all pairs in the array whose sum is equal to ‘sum’.

```Examples :
Input  :  arr[] = {1, 5, 7, -1, 5},
sum = 6
Output : (1, 5) (7, -1) (1, 5)

Input  :  arr[] = {2, 5, 17, -1},
sum = 7
Output :  (2, 5)
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A simple solution is be traverse each element and check if there’s another number in the array which can be added to it to give sum.

## C++

 `// C++ implementation of simple method to ` `// find print pairs with given sum. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns number of pairs in arr[0..n-1] ` `// with sum equal to 'sum' ` `int` `printPairs(``int` `arr[], ``int` `n, ``int` `sum) ` `{ ` `    ``int` `count = 0; ``// Initialize result ` ` `  `    ``// Consider all possible pairs and check ` `    ``// their sums ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` `            ``if` `(arr[i] + arr[j] == sum) ` `                ``cout << ``"("` `<< arr[i] << ``", "` `                     ``<< arr[j] << ``")"` `<< endl; ` `} ` ` `  `// Driver function to test the above function ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 5, 7, -1, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `sum = 6; ` `    ``printPairs(arr, n, sum); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of ` `// simple method to find ` `// print pairs with given sum. ` ` `  `class` `GFG { ` ` `  `    ``// Returns number of pairs ` `    ``// in arr[0..n-1] with sum ` `    ``// equal to 'sum' ` `    ``static` `void` `printPairs(``int` `arr[], ` `                           ``int` `n, ``int` `sum) ` `    ``{ ` `        ``// int count = 0; ` ` `  `        ``// Consider all possible pairs ` `        ``// and check their sums ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``for` `(``int` `j = i + ``1``; j < n; j++) ` `                ``if` `(arr[i] + arr[j] == sum) ` `                    ``System.out.println(``"("` `+ arr[i] + ``", "` `+ arr[j] + ``")"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] arg) ` `    ``{ ` `        ``int` `arr[] = { ``1``, ``5``, ``7``, -``1``, ``5` `}; ` `        ``int` `n = arr.length; ` `        ``int` `sum = ``6``; ` `        ``printPairs(arr, n, sum); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by Smitha `

## Python 3

 `# Python 3 implementation  ` `# of simple method to find ` `# print pairs with given sum. ` ` `  `# Returns number of pairs  ` `# in arr[0..n-1] with sum ` `# equal to 'sum' ` `def` `printPairs(arr, n, ``sum``): ` ` `  `    ``# count = 0  ` ` `  `    ``# Consider all possible  ` `    ``# pairs and check their sums ` `    ``for` `i ``in` `range``(``0``, n ): ` `        ``for` `j ``in` `range``(i ``+` `1``, n ): ` `            ``if` `(arr[i] ``+` `arr[j] ``=``=` `sum``): ` `                ``print``(``"("``, arr[i],  ` `                      ``", "``, arr[j],  ` `                      ``")"``, sep ``=` `"") ` ` `  ` `  `# Driver Code ` `arr ``=` `[``1``, ``5``, ``7``, ``-``1``, ``5``] ` `n ``=` `len``(arr) ` `sum` `=` `6` `printPairs(arr, n, ``sum``) ` ` `  `# This code is contributed  ` `# by Smitha `

## C#

 `// C# implementation of simple ` `// method to find print pairs ` `// with given sum. ` `using` `System; ` ` `  `class` `GFG { ` `    ``// Returns number of pairs ` `    ``// in arr[0..n-1] with sum ` `    ``// equal to 'sum' ` `    ``static` `void` `printPairs(``int``[] arr, ` `                           ``int` `n, ``int` `sum) ` `    ``{ ` `        ``// int count = 0; ` ` `  `        ``// Consider all possible pairs ` `        ``// and check their sums ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``for` `(``int` `j = i + 1; j < n; j++) ` `                ``if` `(arr[i] + arr[j] == sum) ` `                    ``Console.Write(``"("` `+ arr[i] + ``", "` `+ arr[j] + ``")"` `                                  ``+ ``"\n"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 1, 5, 7, -1, 5 }; ` `        ``int` `n = arr.Length; ` `        ``int` `sum = 6; ` `        ``printPairs(arr, n, sum); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by Smitha `

## PHP

 ` `

Output :

```(1, 5)
(1, 5)
(7, -1)
```

Method 2 (Use hashing).
We create an empty hash table. Now we traverse through the array and check for pairs in hash table. If a matching element is found, we print the pair number of times equal to number of occurrences of the matching element.

Note that the worst case of time complexity of this solution is O(c + n) where c is count of pairs with given sum.

## C++

 `// C++ implementation of simple method to ` `// find count of pairs with given sum. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns number of pairs in arr[0..n-1] ` `// with sum equal to 'sum' ` `void` `printPairs(``int` `arr[], ``int` `n, ``int` `sum) ` `{ ` `    ``// Store counts of all elements in map m ` `    ``unordered_map<``int``, ``int``> m; ` ` `  `    ``// Traverse through all elements ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Search if a pair can be formed with ` `        ``// arr[i]. ` `        ``int` `rem = sum - arr[i]; ` `        ``if` `(m.find(rem) != m.end()) { ` `            ``int` `count = m[rem]; ` `            ``for` `(``int` `j = 0; j < count; j++) ` `                ``cout << ``"("` `<< rem << ``", "` `                     ``<< arr[i] << ``")"` `<< endl; ` `        ``} ` `        ``m[arr[i]]++; ` `    ``} ` `} ` ` `  `// Driver function to test the above function ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 5, 7, -1, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `sum = 6; ` `    ``printPairs(arr, n, sum); ` `    ``return` `0; ` `} `

Output :

```(1, 5)
(7, -1)
(1, 5)
```

Method 3.
Another method to Print all pairs with the given sum is given as follows:

## Java

 `import` `java.util.Arrays; ` ` `  `/** ` ` ``* Created by sampat. ` ` ``*/` `public` `class` `SumOfPairs { ` ` `  `    ``public` `void` `pairedElements(``int` `arr[], ``int` `sum) ` `    ``{ ` `        ``int` `low = ``0``; ` `        ``int` `high = arr.length - ``1``; ` ` `  `        ``while` `(low < high) { ` `            ``if` `(arr[low] + arr[high] == sum) { ` `                ``System.out.println(``"The pair is : ("` `                                   ``+ arr[low] + ``", "` `+ arr[high] + ``")"``); ` `            ``} ` `            ``if` `(arr[low] + arr[high] > sum) { ` `                ``high--; ` `            ``} ` `            ``else` `{ ` `                ``low++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``2``, ``3``, ``4``, -``2``, ``6``, ``8``, ``9``, ``11` `}; ` `        ``Arrays.sort(arr); ` ` `  `        ``SumOfPairs sp = ``new` `SumOfPairs(); ` `        ``sp.pairedElements(arr, ``6``); ` `    ``} ` `} `

## C#

 `// C# program to find triplets in a given ` `// array whose sum is equal to given sum. ` `using` `System; ` ` `  `public` `class` `SumOfPairs  ` `{ ` ` `  `    ``public` `void` `pairedElements(``int` `[]arr, ``int` `sum) ` `    ``{ ` `        ``int` `low = 0; ` `        ``int` `high = arr.Length - 1; ` ` `  `        ``while` `(low < high)  ` `        ``{ ` `            ``if` `(arr[low] + arr[high] == sum) ` `            ``{ ` `                ``Console.WriteLine(``"The pair is : ("` `                                ``+ arr[low] + ``", "` `+ arr[high] + ``")"``); ` `            ``} ` `            ``if` `(arr[low] + arr[high] > sum) ` `            ``{ ` `                ``high--; ` `            ``} ` `            ``else`  `            ``{ ` `                ``low++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int` `[]arr = { 2, 3, 4, -2, 6, 8, 9, 11 }; ` `        ``Array.Sort(arr); ` ` `  `        ``SumOfPairs sp = ``new` `SumOfPairs(); ` `        ``sp.pairedElements(arr, 6); ` `    ``} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output :

```The pair is : (-2, 8)
The pair is : (2, 4)
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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 :

5

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