BudiBadu Logo

Even Count

Array Easy 1 views
Like1

Ever needed to pick out just the even numbers from a group of items? That’s what Even Count is all about! You’re given an array of integers, and your task is to count how many of those numbers are Even (meaning they are exactly divisible by 2).

The "secret sauce" here is the Modulo Operator (%). This handy tool acts like a sieve: it checks if number % 2 == 0. If it’s true, you increment your counter and keep moving. It’s a simple, effective way to filter data based on divisibility. Think of it like a quality check: you inspect each number, and if it meets your "even" criteria, it gets a tally mark.

This challenge is a great way to practice loops and basic conditionals. It’s a simple exercise that builds the core logic you’ll need for more advanced filtering and data sorting tasks!

Because this is a counting/optimization-style challenge, dynamic programming is usually the safest approach: define what each index or state means, initialize valid base states, and make transitions explicit. If the problem uses modulo arithmetic, apply modulo at every accumulation step so large intermediate totals never corrupt the final answer.

Examples

Example 1
Input
nums = [1, 2, 3, 4, 5, 6]
Output
3
Explanation

Example 1: There are 3 even numbers (2, 4, 6)

Example 2
Input
nums = [1, 3, 5]
Output
0
Explanation

Example 2: No even numbers in [1, 3, 5]

Example 3
Input
nums = [2, 4, 6, 8]
Output
4
Explanation

Example 3: All 4 numbers are even

Algorithm Flow

Recommendation Algorithm Flow for Even Count - Budibadu
Recommendation Algorithm Flow for Even Count - Budibadu

Best Answers

java
class Solution {
    public int count_even(int[] nums) {
        int count = 0;
        for (int num : nums) {
            if (num % 2 == 0) count++;
        }
        return count;
    }
}