Implement the method countEvenNumbers that returns how many even numbers are in an array.
Count how many even integers appear in an array. An even number is divisible by 2 with no remainder.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Zero should be counted as even.
- The array may contain positive and negative values.
Example 1
Input
nums (int[]) =
[1, 2, 3, 4, 5]size (int) =
5Returns (int)
2Example 2
Input
nums (int[]) =
[2, 4, 6]size (int) =
3Returns (int)
3Example 3
Input
nums (int[]) =
[1, 3, 5]size (int) =
3Returns (int)
0Example 4
Input
nums (int[]) =
[-2, -1, 0, 7]size (int) =
4Returns (int)
2Loop through the requested range and increase the answer whenever value % 2 == 0.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run