Given an unsorted array of integers. The task is to find any two non-overlapping pairs whose sum is equal.

Two pairs are said to be non-overlapping if all the elements of the pairs are at different indices. That is, pair **(A _{i}, A_{j})** and pair

**(A**are said to be non-overlapping if

_{m}, A_{n})**i ≠ j ≠ m ≠ n**.

**Examples**:

Input: arr[] = {8, 4, 7, 8, 4} Output: Pair First(4, 8) Pair Second (8, 4) Input: arr[] = {8, 4, 7, 4} Output: No such non-overlapping pairs present.Note: (8, 4) and (8, 4) forms overlapping pair as index of 8 is same in both pairs.

The idea is to use a map of key-value pair to store all occurrences of a sum. The key in the map will store the sum and the corresponding value will be a list of pair of indices (i, j) with that sum.

- The idea is to traverse the array and generate all possible pairs.
- If a new sum is found, insert it directly to the map otherwise check if any of the previously encountered pairs with that sum does not overlap with current pair.
- If not, print both of the pairs.

Below is implementation of the above approach:

## C++

`// CPP programs to find two non-overlapping ` `// pairs having equal sum in an Array ` ` ` `#include <iostream> ` `#include <unordered_map> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `typedef` `pair<` `int` `, ` `int` `> Pair; ` ` ` `// Function to find two non-overlapping ` `// with same sum in an array ` `void` `findPairs(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// first create an empty map ` ` ` `// key -> which is sum of a pair of ` ` ` `// elements in the array ` ` ` `// value -> vector storing index of ` ` ` `// every pair having that sum ` ` ` `unordered_map<` `int` `, vector<Pair> > map; ` ` ` ` ` `// consider every pair (arr[i], arr[j]) ` ` ` `// and where (j > i) ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` ` ` `// calculate sum of current pair ` ` ` `int` `sum = arr[i] + arr[j]; ` ` ` ` ` `// if sum is already present in the map ` ` ` `if` `(map.find(sum) != map.end()) { ` ` ` ` ` `// check every pair having equal sum ` ` ` `for` `(` `auto` `pair : map.find(sum)->second) { ` ` ` `int` `m = pair.first, n = pair.second; ` ` ` ` ` `// if pairs don't overlap, ` ` ` `// print them and return ` ` ` `if` `((m != i && m != j) && (n != i && n != j)) { ` ` ` `cout << ` `"Pair First("` `<< arr[i] << ` `", "` ` ` `<< arr[j] << ` `")\nPair Second ("` ` ` `<< arr[m] << ` `", "` `<< arr[n] << ` `")"` `; ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Insert current pair into the map ` ` ` `map[sum].push_back({ i, j }); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If no such pair found ` ` ` `cout << ` `"No such non-overlapping pairs present"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 8, 4, 7, 8, 4 }; ` ` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `findPairs(arr, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find two non-overlapping ` `// pairs having equal sum in an Array ` ` ` `import` `java.util.ArrayList; ` `import` `java.util.HashMap; ` `import` `java.util.List; ` `import` `java.util.Map; ` ` ` `// Declare a pair ` `class` `Pair { ` ` ` `public` `int` `x, y; ` ` ` ` ` `Pair(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `this` `.x = x; ` ` ` `this` `.y = y; ` ` ` `} ` `} ` ` ` `class` `GFG { ` ` ` `// Function to find two non-overlapping ` ` ` `// pairs with same sum in an array ` ` ` `public` `static` `void` `findPairs(` `int` `[] A) ` ` ` `{ ` ` ` `// first create an empty map ` ` ` `// key -> which is sum of a pair ` ` ` `// of elements in the array ` ` ` `// value -> list storing index of ` ` ` `// every pair having that sum ` ` ` `Map<Integer, List<Pair> > map = ` `new` `HashMap<>(); ` ` ` ` ` `// consider every pair (A[i], A[j]) where (j > i) ` ` ` `for` `(` `int` `i = ` `0` `; i < A.length - ` `1` `; i++) { ` ` ` `for` `(` `int` `j = i + ` `1` `; j < A.length; j++) { ` ` ` ` ` `// calculate sum of current pair ` ` ` `int` `sum = A[i] + A[j]; ` ` ` ` ` `// if sum is already present in the map ` ` ` `if` `(map.containsKey(sum)) { ` ` ` ` ` `// check every pair having desired sum ` ` ` `for` `(Pair pair : map.get(sum)) { ` ` ` `int` `x = pair.x; ` ` ` `int` `y = pair.y; ` ` ` ` ` `// if pairs don't overlap, print ` ` ` `// them and return ` ` ` `if` `((x != i && x != j) && (y != i && y != j)) { ` ` ` `System.out.println(` `"Pair First("` `+ A[i] + ` `", "` ` ` `+ A[j] + ` `")"` `); ` ` ` ` ` `System.out.println(` `"Pair Second ("` `+ A[x] + ` `", "` ` ` `+ A[y] + ` `")"` `); ` ` ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Insert current pair into the map ` ` ` `map.putIfAbsent(sum, ` `new` `ArrayList<>()); ` ` ` `map.get(sum).add(` `new` `Pair(i, j)); ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.print(` `"No such non-overlapping pairs present"` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] A = { ` `8` `, ` `4` `, ` `7` `, ` `8` `, ` `4` `}; ` ` ` ` ` `findPairs(A); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 programs to find two non-overlapping ` `# pairs having equal Sum in an Array ` ` ` `# Function to find two non-overlapping ` `# with same Sum in an array ` `def` `findPairs(arr, size): ` ` ` ` ` `# first create an empty Map ` ` ` `# key -> which is Sum of a pair of ` ` ` `# elements in the array ` ` ` `# value -> vector storing index of ` ` ` `# every pair having that Sum ` ` ` `Map` `=` `{} ` ` ` ` ` `# consider every pair (arr[i], arr[j]) ` ` ` `# and where (j > i) ` ` ` `for` `i ` `in` `range` `(` `0` `, size ` `-` `1` `): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, size): ` ` ` ` ` `# calculate Sum of current pair ` ` ` `Sum` `=` `arr[i] ` `+` `arr[j] ` ` ` ` ` `# if Sum is already present in the Map ` ` ` `if` `Sum` `in` `Map` `: ` ` ` ` ` `# check every pair having equal Sum ` ` ` `for` `pair ` `in` `Map` `[` `Sum` `]: ` ` ` `m, n ` `=` `pair ` ` ` ` ` `# if pairs don't overlap, ` ` ` `# print them and return ` ` ` `if` `((m !` `=` `i ` `and` `m !` `=` `j) ` `and` ` ` `(n !` `=` `i ` `and` `n !` `=` `j)): ` ` ` ` ` `print` `(` `"Pair First ({}, {})"` `. ` ` ` `format` `(arr[i], arr[j])) ` ` ` `print` `(` `"Pair Second ({}, {})"` `. ` ` ` `format` `(arr[m], arr[n])) ` ` ` `return` ` ` ` ` `# Insert current pair into the Map ` ` ` `if` `Sum` `not` `in` `Map` `: ` ` ` `Map` `[` `Sum` `] ` `=` `[] ` ` ` `Map` `[` `Sum` `].append((i, j)) ` ` ` ` ` `# If no such pair found ` ` ` `print` `(` `"No such non-overlapping pairs present"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[` `8` `, ` `4` `, ` `7` `, ` `8` `, ` `4` `] ` ` ` `size ` `=` `len` `(arr) ` ` ` ` ` `findPairs(arr, size) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

Pair First(4, 8) Pair Second (8, 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.

## Recommended Posts:

- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count pairs from two arrays having sum equal to K
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Number of pairs in an array having sum equal to product
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Count of all possible pairs having sum of LCM and GCD equal to N
- Maximum Count of pairs having equal Sum based on the given conditions
- Count of pairs having each element equal to index of the other from an Array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Count distinct pairs from two arrays having same sum of digits
- Find an element in array such that sum of left array is equal to sum of right array
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Count number of pairs in array having sum divisible by K | SET 2
- Largest subset having with sum less than equal to sum of respective indices
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count pairs from two linked lists whose sum is equal to a given value
- Count the number of ways to divide an array into three contiguous parts having equal sum

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.