We have an array of integers and we have to find two such elements in the array such that sum of these two elements is equal to the sum of rest of elements in array.

**Examples:**

Input : arr[] = {2, 11, 5, 1, 4, 7} Output : Elements are 4 and 11 Note that 4 + 11 = 2 + 5 + 1 + 7 Input : arr[] = {2, 4, 2, 1, 11, 15} Output : Elements do not exist

A **simple solution** is to consider every pair one by one, find its sum and compare the sum with sum of rest of the elements. If we find a pair whose sum is equal to rest of elements, we print the pair and return true. Time complexity of this solution is O(n^{3})

An **efficient solution** is to find sum of all array elements. Let this sum be “sum”. Now the task reduces to finding a pair with sum equals to sum/2.

Another optimization is, a pair can exist only if the sum of whole array is even because we are basically dividing it into two parts with equal sum.

**1-** Find the sum of whole array. Let this sum be “sum”

**2-** If sum is odd, return false.

**3-** Find a pair with sum equals to “sum/2” using hashing based method discussed here as method 2. If a pair is found, print it and return true.

**4-** If no pair exists, return false.

Below is the implementation of above steps.

## C++

`// C++ program to find whether two elements exist ` `// whose sum is equal to sum of rest of the elements. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check whether two elements exist ` `// whose sum is equal to sum of rest of the elements. ` `bool` `checkPair(` `int` `arr[],` `int` `n) ` `{ ` ` ` `// Find sum of whole array ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `// If sum of array is not even than we can not ` ` ` `// divide it into two part ` ` ` `if` `(sum%2 != 0) ` ` ` `return` `false` `; ` ` ` ` ` `sum = sum/2; ` ` ` ` ` `// For each element arr[i], see if there is ` ` ` `// another element with vaalue sum - arr[i] ` ` ` `unordered_set<` `int` `> s; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `int` `val = sum-arr[i]; ` ` ` ` ` `// If element exist than return the pair ` ` ` `if` `(s.find(val) != s.end()) ` ` ` `{ ` ` ` `printf` `(` `"Pair elements are %d and %d\n"` `, ` ` ` `arr[i], val); ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `s.insert(arr[i]); ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver program. ` `int` `main() ` `{ ` ` ` `int` `arr[] = {2, 11, 5, 1, 4, 7}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `if` `(checkPair(arr, n) == ` `false` `) ` ` ` `printf` `(` `"No pair found"` `); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find whether ` `# two elements exist whose sum is ` `# equal to sum of rest of the elements. ` ` ` `# Function to check whether two ` `# elements exist whose sum is equal ` `# to sum of rest of the elements. ` `def` `checkPair(arr, n): ` ` ` `s ` `=` `set` `() ` ` ` `sum` `=` `0` ` ` ` ` `# Find sum of whole array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sum` `+` `=` `arr[i] ` ` ` ` ` `# / If sum of array is not ` ` ` `# even than we can not ` ` ` `# divide it into two part ` ` ` `if` `sum` `%` `2` `!` `=` `0` `: ` ` ` `return` `False` ` ` `sum` `=` `sum` `/` `2` ` ` ` ` `# For each element arr[i], see if ` ` ` `# there is another element with ` ` ` `# value sum - arr[i] ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `val ` `=` `sum` `-` `arr[i] ` ` ` `if` `arr[i] ` `not` `in` `s: ` ` ` `s.add(arr[i]) ` ` ` ` ` `# If element exist than ` ` ` `# return the pair ` ` ` `if` `val ` `in` `s: ` ` ` `print` `(` `"Pair elements are"` `, ` ` ` `arr[i], ` `"and"` `, ` `int` `(val)) ` ` ` `# Driver Code ` `arr ` `=` `[` `2` `, ` `11` `, ` `5` `, ` `1` `, ` `4` `, ` `7` `] ` `n ` `=` `len` `(arr) ` `if` `checkPair(arr, n) ` `=` `=` `False` `: ` ` ` `print` `(` `"No pair found"` `) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

Pair elements are 4 and 11

**Time complexity :** O(n). unordered_set is implemented using hashing. Time complexity hash search and insert is assumed as O(1) here.

This article is contributed by **Niteesh kumar**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Maximum difference between group of k-elements and rest of the array.
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Remove minimum number of elements such that no common element exist in both array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Find pairs in array whose sums already exist in array
- XOR of all elements of array with set bits equal to K
- Count of smaller or equal elements in sorted array
- Adding elements of an array until every element becomes greater than or equal to k
- Count pairs in an array such that both elements has equal set bits
- Minimum operation to make all elements equal in array
- Minimum cost to equal all elements of array using two operation
- Sum of elements in an array with frequencies greater than or equal to that element
- Count elements less than or equal to a given value in a sorted rotated array