beginnerArray ProblemsJava

Count the Even and Odd Numbers in an Array

Count how many elements of an array are even and how many are odd.

Quick Answer

Keep two counters, one for evens and one for odds. Loop through the array and test each element with element % 2 == 0. Increment the even counter when the test passes and the odd counter otherwise. After the loop the two counters hold the answer.

Problem Statement

Given an array of integers, count how many elements are even and how many are odd. A number is even when it is divisible by 2 and odd otherwise.

Return (or print) both counts. Handle negative numbers correctly — parity is determined by the last binary bit, not by the sign.

Input: An array arr of length n.

Output: Two counts: the number of even elements and the number of odd elements.

Examples

Example 1
Input:  [1, 2, 3, 4, 5]
Output: Even: 2, Odd: 3

Evens are 2 and 4 (count 2); odds are 1, 3 and 5 (count 3).

Example 2
Input:  [10, 15, 20, 25]
Output: Even: 2, Odd: 2

Evens are 10 and 20; odds are 15 and 25.

Constraints

  • 0 <= n <= 10^6
  • Elements may be negative or zero (zero is even)

Think Before You Code

Reveal the questions to ask yourself first
  • How do you test whether a single number is even?
  • How many counters do you need, and what should they start at?
  • Does % 2 behave correctly for negative numbers here?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You need two tallies that you bump as you scan the array.
Hint 2
A number is even when `x % 2 == 0`; otherwise it is odd.
Hint 3
Start `even = 0` and `odd = 0`, then in one loop increment the matching counter for each element.

Approach

Reveal the step-by-step approach

Tally the two parities in a single pass.

  1. Set even = 0 and odd = 0.
  2. For each element x:
    • If x % 2 == 0, increment even.
    • Otherwise increment odd.
  3. Report both counts.

Testing x % 2 == 0 is reliable even for negatives, because for an odd negative like -3 the expression -3 % 2 is -1, which is not 0, so it is correctly counted as odd.

Dry Run

Walk through the example step by step

Counting [1, 2, 3, 4, 5]:

x | x % 2 | bucket | even | odd
--+-------+--------+------+----
1 | 1     | odd    | 0    | 1
2 | 0     | even   | 1    | 1
3 | 1     | odd    | 1    | 2
4 | 0     | even   | 2    | 2
5 | 1     | odd    | 2    | 3
------------------------ Even: 2, Odd: 3

Solution

Reveal the full Java solution
public class CountEvenOdd {
    public static int[] countEvenOdd(int[] arr) {
        int even = 0;
        int odd = 0;
        for (int x : arr) {
            if (x % 2 == 0) {
                even++;
            } else {
                odd++;
            }
        }
        return new int[]{even, odd};
    }

    public static void main(String[] args) {
        int[] r1 = countEvenOdd(new int[]{1, 2, 3, 4, 5});
        System.out.println("Even: " + r1[0] + ", Odd: " + r1[1]); // Even: 2, Odd: 3

        int[] r2 = countEvenOdd(new int[]{10, 15, 20, 25});
        System.out.println("Even: " + r2[0] + ", Odd: " + r2[1]); // Even: 2, Odd: 2
    }
}

Each element falls into exactly one bucket, so a single if/else on x % 2 == 0 correctly splits the counts in one O(n) pass. Testing against 0 (rather than testing x % 2 == 1) is what keeps negatives correct, since -3 % 2 is -1 in Java rather than 1.

Time: O(n)Space: O(1)

Common Mistakes

  • Testing `x % 2 == 1` for odds, which fails for negative odd numbers because `-3 % 2` is -1.
  • Forgetting that 0 is even and mis-bucketing it.
  • Using a single counter and trying to derive the other, then getting an off-by-one on empty arrays.

Edge Cases to Test

  • Empty array [] — both counts are 0.
  • Array containing 0, which must be counted as even.
  • Negative numbers such as [-2, -3], where -2 is even and -3 is odd.

Interview Follow-Ups

  • How would you also compute the sum of the even elements separately?
  • How would you count evens and odds using bitwise `& 1` instead of `% 2`?
  • How would you partition the array so all evens come before all odds?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us