Sum of Even Numbers
easySave
ArrayMath
Given an array of integers, compute the sum of all the even numbers in the array.
Example 1
Input
[ 2, 5, 3, 8, 12 ]
Output
22
Explanation. The even numbers in the array are 2, 8, and 12. Their sum is 22.
Example 2
Input
[ 7, 13, 10 ]
Output
10
Explanation. The only even number in the array is 10.
Example 3
Input
[ -2, -3, -6 ]
Output
-8
Explanation. The even numbers are -2 and -6, and their sum is -8.
Follow-up: What would be the most optimal way to execute this operation if the input size is very large?
Constraints:
The input array can contain both positive and negative integers, but will always have at least one element.
- Views
- 2