Element equal to the sum of all the remaining elements

Given an array of N positive elements. The task is to find an element which is equal to the sum of all elements of array except itself.

Examples:

Input: arr[] = {1, 2, 3, 6}
Output: 6
6 is the element which is equal to the sum of all 
remaining elements i.e. 1 + 2+ 3 = 6

Input: arr[] = {2, 2, 2, 2}
Output: -1

Approach: First of all, find the sum of all elements of an array. Then iterate over each element and check the condition that if (a[i] == sum-a[i] ). If true then print that a[i], else print “-1” if no such element found.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
  
// Function to find the element
int findEle(int arr[], int n)
{
    // sum is use to store
    // sum of all elements
    // of array
    ll sum = 0;
  
    for (int i = 0; i < n; i++)
        sum += arr[i];
  
    // iterate over all elements
    for (int i = 0; i < n; i++)
        if (arr[i] == sum - arr[i])
            return arr[i];
  
    return -1;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findEle(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
  
import java.io.*;
  
class GFG {
  
  
// Function to find the element
static int findEle(int arr[], int n)
{
    // sum is use to store
    // sum of all elements
    // of array
    int sum = 0;
  
    for (int i = 0; i < n; i++)
        sum += arr[i];
  
    // iterate over all elements
    for (int i = 0; i < n; i++)
        if (arr[i] == sum - arr[i])
            return arr[i];
  
    return -1;
}
  
// Driver code
  
    public static void main (String[] args) {
        int arr[] = { 1, 2, 3, 6 };
    int n = arr.length;
    System.out.print(findEle(arr, n));
    }
}
// This code is contributed by shs..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of 
# the above approach
  
# Function to find the element
def findEle(arr, n) :
      
    # sum is use to store 
    # sum of all elements 
    # of array 
    sum = 0
      
    for i in range(n) :
        sum += arr[i]
      
    # iterate over all elements
    for i in range(n) :
        if arr[i] == sum - arr[i] :
            return arr[i]
      
    return -1
  
# Driver Code
if __name__ == "__main__" :
      
    arr = [1, 2, 3, 6 ]
    n = len(arr)
    print(findEle(arr, n))
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the 
// above approach 
using System; 
  
class GFG 
  
// Function to find the element 
static int findEle(int []arr, int n) 
    // sum is use to store 
    // sum of all elements 
    // of array 
    int sum = 0; 
  
    for (int i = 0; i < n; i++) 
        sum += arr[i]; 
  
    // iterate over all elements 
    for (int i = 0; i < n; i++) 
        if (arr[i] == sum - arr[i]) 
            return arr[i]; 
  
    return -1; 
  
// Driver code 
static public void Main (String []args)
    int []arr = { 1, 2, 3, 6 }; 
    int n = arr.Length; 
    Console.WriteLine(findEle(arr, n)); 
  
// This code is contributed 
// by Arnab Kundu

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above approach
  
// Function to find the element
function findEle($arr, $n)
{
    // sum is use to store
    // sum of all elements
    // of array
    $sum = 0;
  
    for ($i = 0; $i < $n; $i++)
        $sum += $arr[$i];
  
    // iterate over all elements
    for ($i = 0; $i < $n; $i++)
        if ($arr[$i] == $sum - $arr[$i])
            return $arr[$i];
  
    return -1;
}
  
// Driver code
$arr= array(1, 2, 3, 6 );
$n = sizeof($arr);
echo findEle($arr, $n);
  
// This code is contributed 
// by Akanksha Rai
?>

chevron_right


Output:

6

Note: Above problem can be solved with the concept used in Check if the array has an element which is equal to sum of all the remaining elements.



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.