# Compare sum of first N-1 elements to Nth element of an array

Given an array arr[] of size N, the task is to check whether the sum of first N – 1 element of the array is equal to the last element.

Examples:

Input: arr[] = {1, 2, 3, 4, 10}
Output: Yes

Input: arr[] = {1, 2, 3, 4, 12}
Output: No

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Find the sum of the first N – 1 elements from the array i.e. arr + arr + … + arr[N – 2] and compare it with arr[N – 1].

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if sum of ` `// first n-1 elements of the array is ` `// equal to the last element ` `bool` `isSumEqual(``int` `ar[], ``int` `n) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// Find the sum of first n-1 ` `    ``// elements of the array ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``sum += ar[i]; ` ` `  `    ``// If sum equals to the last element ` `    ``if` `(sum == ar[n - 1]) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 3, 4, 10 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``if` `(isSumEqual(arr, n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function that returns true if sum of ` `    ``// first n-1 elements of the array is ` `    ``// equal to the last element ` `    ``static` `boolean` `isSumEqual(``int` `ar[], ``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``; ` ` `  `        ``// Find the sum of first n-1 ` `        ``// elements of the array ` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `            ``sum += ar[i]; ` ` `  `        ``// If sum equals to the last element ` `        ``if` `(sum == ar[n - ``1``]) ` `            ``return` `true``; ` ` `  `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``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"``); ` `    ``} ` `} ` ` `  `// This code is contributed by jit_t. `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function that returns true if sum of ` `# first n-1 elements of the array is ` `# equal to the last element ` `def` `isSumEqual(ar, n): ` `    ``sum` `=` `0` ` `  `    ``# Find the sum of first n-1 ` `    ``# elements of the array ` `    ``for` `i ``in` `range``(n ``-` `1``): ` `        ``sum` `+``=` `ar[i] ` ` `  `    ``# If sum equals to the last element ` `    ``if` `(``sum` `=``=` `ar[n ``-` `1``]): ` `        ``return` `True` ` `  `    ``return` `False` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``10``] ` `    ``n ``=` `len``(arr) ` ` `  `    ``if` `(isSumEqual(arr, n)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` `         `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function that returns true if sum of ` `    ``// first n-1 elements of the array is ` `    ``// equal to the last element ` `    ``static` `bool` `isSumEqual(``int``[] ar, ``int` `n) ` `    ``{ ` `        ``int` `sum = 0; ` ` `  `        ``// Find the sum of first n-1 ` `        ``// elements of the array ` `        ``for` `(``int` `i = 0; i < n - 1; i++) ` `            ``sum += ar[i]; ` ` `  `        ``// If sum equals to the last element ` `        ``if` `(sum == ar[n - 1]) ` `            ``return` `true``; ` ` `  `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 1, 2, 3, 4, 10 }; ` `        ``int` `n = arr.Length; ` ` `  `        ``if` `(isSumEqual(arr, n)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by ajit `

## PHP

 ` `

Output:

```Yes
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.