Check if the array has an element which is equal to sum of all the remaining elements | C # program 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 ; Storing frequency in map ; Stores the sum ; Traverse the array and count the array elements ; Only possible if sum is even ; If half sum is available ; Driver code
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 " ) ; } }