Sum of Even Numbers
easySave
ArrayMath
Given an array of integers, return the sum of all the even numbers in the array.
Example 1
Input
[ 1, 2, 3, 4, 5 ]
Output
6
Explanation. The even numbers in the array are 2 and 4, and their sum is 6.
Example 2
Input
[ -2, -1, -3, -4 ]
Output
-6
Explanation. The even numbers in the array are -2 and -4, and their sum is -6.
Example 3
Input
[ 0, 1, 23, 45, 66 ]
Output
66
Explanation. The only even number in the array is 66.
Follow-up: Can you solve the problem in a single pass through the array? What is the time complexity of your solution?
Constraints:
The array will contain at least one integer and may contain positive, negative, or zero values.
- Views
- 3