Sum of Positive Even Numbers
easySave
ArrayMath
Given an array of integers, compute the sum of all the even numbers in the array that are positive.
Example 1
Input
[ 1, 2, 3, 4, 5, -6 ]
Output
6
Explanation. The positive even numbers in the array are 2 and 4. Their sum is 6.
Example 2
Input
[ -2, -4, -6 ]
Output
0
Explanation. There are no positive even numbers in the array. Thus, the sum is 0.
Example 3
Input
[ 0, 8, 5, 12, 7 ]
Output
20
Explanation. The positive even numbers in the array are 8 and 12. Their sum is 20.
Example 4
Input
[ 11, 22, 33, 44, 55 ]
Output
66
Explanation. The positive even numbers in the array are 22 and 44. Their sum is 66.
Follow-up: How would you optimize your solution if the input array is very large?
Constraints:
The array may contain both positive and negative integers, as well as zero. The array will contain at least one integer.
- Views
- 3