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 that 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++
#include <bits/stdc++.h>
using namespace std;
bool isExists( int a[], int n)
{
unordered_map< int , int > freq;
int sum = 0;
for ( int i = 0; i < n; i++) {
freq[a[i]]++;
sum += a[i];
}
if (sum % 2 == 0) {
if (freq[sum / 2])
return true ;
}
return false ;
}
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;
}
|
Java
import java.util.*;
class Solution{
static boolean isExists( int a[], int n)
{
Map<Integer, Integer> freq= new HashMap<Integer, Integer>();
int sum = 0 ;
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];
}
if (sum % 2 == 0 ) {
if (freq.get(sum / 2 )!= null )
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def isExists(a, n):
freq = {i : 0 for i in a}
Sum = 0
for i in range (n):
freq[a[i]] + = 1
Sum + = a[i]
if Sum % 2 = = 0 :
if freq[ Sum / / 2 ]:
return True
return False
a = [ 5 , 1 , 2 , 2 ]
n = len (a)
if isExists(a, n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Solution
{
static Boolean isExists( int []arr, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
int sum = 0;
for ( int i = 0; i < n; i++)
{
if (m.ContainsKey(arr[i]))
{
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else
{
m.Add(arr[i], 1);
}
sum += arr[i];
}
if (sum % 2 == 0)
{
if (m[sum / 2] != 0)
return true ;
}
return false ;
}
public static void Main()
{
int []a = { 5, 1, 2, 2 };
int n = a.Length;
if (isExists(a, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isExists(a, n)
{
let freq= new Map();
let sum = 0;
for (let i = 0; i < n; i++) {
freq.set(a[i],freq.get(a[i])== null ?0:freq.get(a[i])+1);
sum += a[i];
}
if (sum % 2 == 0) {
if (freq.get(sum / 2)!= null )
return true ;
}
return false ;
}
let a = [ 5, 1, 2, 2 ];
let n = a.length;
if (isExists(a, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Another Approach: Calculate the total sum of all the elements in the array. Then run a FOR-loop to check if each element * 2 == total. If any such element is found, return True, else False at the end of the loop.
- Traverse the array and calculate the total sum of array elements
- Check if the array has an element that is equal to the sum of all the remaining elements.
- Return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isExists( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
for ( int i = 0; i < n; i++) {
if (sum - arr[i] == arr[i])
return true ;
}
return false ;
}
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;
}
|
Java
import java.util.*;
public class GFG {
static boolean isExists( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
}
for ( int i = 0 ; i < n; i++) {
if (sum - arr[i] == arr[i])
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def isExists(arr, n):
sum = 0
for i in range (n):
sum + = arr[i]
for i in range (n):
if sum - arr[i] = = arr[i]: return True
return False
a = [ 5 , 1 , 2 , 2 ]
n = len (a)
if isExists(a,n): print ( 'Yes' )
else : print ( 'No' )
|
C#
using System;
public class GFG {
static bool isExists( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
for ( int i = 0; i < n; i++) {
if (sum - arr[i] == arr[i])
return true ;
}
return false ;
}
public static void Main( string [] args)
{
int [] a = { 5, 1, 2, 2 };
int n = a.Length;
if (isExists(a, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isExists(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
}
for (let i = 0; i < n; i++) {
if (sum - arr[i] == arr[i])
return true ;
}
return false ;
}
let a = [ 5, 1, 2, 2 ];
let n = a.length;
if (isExists(a, n))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N), where N is the length of the given array.
Auxiliary Space: O(1)
Last Updated :
13 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...