Sum of Even Numbers
easySave
ArrayMath
Given an array of integers, calculate the sum of all the even numbers in the array and return that sum.
Example 1
Input
[ 2, 5, 6, 8, 3 ]
Output
16
Explanation. The even numbers in the array are 2, 6, and 8. Their sum is 16.
Example 2
Input
[ -2, 0, 1, 3 ]
Output
-2
Explanation. The even numbers are -2 and 0. Their sum is -2.
Example 3
Input
[ 1, 3, 5, 7 ]
Output
0
Explanation. There are no even numbers in the array, so the sum is 0.
Follow-up: Try to optimize your solution to have a linear time complexity.
Constraints:
The input array may contain both positive and negative integers, but it will always contain at least one integer.
- Views
- 3