Compare sum of first N | Java implementation of the approach ; Function that returns true if sum of first n - 1 elements of the array is equal to the last element ; Find the sum of first n - 1 elements of the array ; If sum equals to the last element ; Driver code
import java . io . * ; class GFG { static boolean isSumEqual ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) sum += ar [ i ] ; if ( sum == ar [ n - 1 ] ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 10 } ; int n = arr . length ; if ( isSumEqual ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }