# Check if the array has an element which is equal to sum of all the remaining elements

Given an array of N elements, the task is to check if the array has an element which is equal to the sum of all the remaining elements.

**Examples**:

Input: a[] = {5, 1, 2, 2}Output: Yes we can write 5=(1+2+2)Input: a[] = {2, 1, 2, 4, 3}Output: No

**Approach:** Suppose that the total elements in the array is **N**. Now, if there exists any such element such that the element is equal to the sum of remaining elements then it can be said that the array can be divided into two halves with equal sum such that one half has only one element with value sum/2.

Also, since both halves have equal sum, the overall sum of the array must be even as we know that:

- ODD + ODD = EVEN
- EVEN + EVEN = EVEN

**Algorithm**:

- Iterate over the array, and count the occurrence of all the elements and store in a map. Also summate the array elements.
- The condition given in the problem is only possible when the below conditions are met.
- Total Sum of the array is even
- sum/2 occurrence in the array should be equal to atleast 1.

- If the above conditions are not met, hence it is not possible to remove any such element.

Below is the implementation of the above approach:

## C++

`// C++ program to Check if the array ` `// has an element which is equal to sum ` `// of all the remaining elements ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if such element exists or not ` `bool` `isExists(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Storing frequency in map ` ` ` `unordered_map<` `int` `, ` `int` `> freq; ` ` ` ` ` `// Stores the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Traverse the array and count the ` ` ` `// array elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `freq[a[i]]++; ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Only possible if sum is even ` ` ` `if` `(sum % 2 == 0) { ` ` ` `// If half sum is available ` ` ` `if` `(freq[sum / 2]) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 5, 1, 2, 2 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `if` `(isExists(a, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Check if the array ` `// has an element which is equal to sum ` `// of all the remaining elements ` `import` `java.util.*; ` `class` `Solution{ ` ` ` `// Function to check if such element exists or not ` `static` `boolean` `isExists(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Storing frequency in map ` ` ` `Map<Integer, Integer> freq= ` `new` `HashMap<Integer, Integer>(); ` ` ` ` ` `// Stores the sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Traverse the array and count the ` ` ` `// array elements ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `freq.put(a[i],freq.get(a[i])==` `null` `?` `0` `:freq.get(a[i])+` `1` `); ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Only possible if sum is even ` ` ` `if` `(sum % ` `2` `== ` `0` `) { ` ` ` `// If half sum is available ` ` ` `if` `(freq.get(sum / ` `2` `)!=` `null` `) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `a[] = { ` `5` `, ` `1` `, ` `2` `, ` `2` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `if` `(isExists(a, n)) ` ` ` `System.out.println( ` `"Yes"` `); ` ` ` `else` ` ` `System.out.println( ` `"No"` `); ` ` ` `} ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

# Python3 code to check if the array has

# an element which is equal to sum of all

# the remaining elements

# function to check if such element

# exists or not

def isExists(a, n):

# storing frequency in dict

freq = {i : 0 for i in a}

#stores the sum

Sum = 0

# traverse the array and count

# the array element

for i in range(n):

freq[a[i]] += 1

Sum += a[i]

# Only possible if sum is even

if Sum % 2 == 0:

#if half sum is available

if freq[Sum // 2]:

return True

return False

# Driver code

a = [5, 1, 2, 2]

n = len(a)

if isExists(a, n):

print(“Yes”)

else:

print(“No”)

# This code is contributed

# by Mohit Kumar

**Output:**

Yes

**Time Complexity:** O(N * log N)

**Auxiliary Space:** O(N)

## Recommended Posts:

- Check if the array has an element which is equal to product of remaining elements
- Element equal to the sum of all the remaining elements
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- 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
- Sum of elements in an array with frequencies greater than or equal to that element
- Adding elements of an array until every element becomes greater than or equal to k
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Average of remaining elements after removing K largest and K smallest elements from array
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Check if elements of array can be made equal by multiplying given prime numbers
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find an element in array such that sum of left array is equal to sum of right array
- Check whether it is possible to make both arrays equal by modifying a single element
- Equal sum array partition excluding a given element

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.