This problem is very direct: count how many numbers in the array are even. A number is even if dividing it by 2 leaves no remainder.
You are not summing the even numbers or returning their positions. The only thing you need is the total count of values that satisfy the even-number rule.
For example, if nums = [1,2,3,4,5,6], the answer is 3 because 2, 4, and 6 are even. If nums = [1,3,5], the answer is 0 because none of the numbers are even. If every number is even, then the answer is just the array length.
So the task is to scan the array and count how many elements satisfy num % 2 === 0.
Example Input & Output
Example 1: There are 3 even numbers (2, 4, 6)
Example 2: No even numbers in [1, 3, 5]
Example 3: All 4 numbers are even
Algorithm Flow

Solution Approach
The simplest approach is a single pass through the array with a counter. Every time we see an even number, we add 1 to that counter.
We start from zero:
Then check each number:
The condition num % 2 === 0 means the number divides evenly by 2, so it belongs in the count.
At the end, return the total:
This runs in O(n) time and uses O(1) extra space.
Best Answers
class Solution {
public int count_even(int[] nums) {
int count = 0;
for (int num : nums) {
if (num % 2 == 0) count++;
}
return count;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
