Count the Even and Odd Numbers in an Array
Count how many elements of an array are even and how many are odd.
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
Input: [1, 2, 3, 4, 5]
Output: Even: 2, Odd: 3Evens are 2 and 4 (count 2); odds are 1, 3 and 5 (count 3).
Input: [10, 15, 20, 25]
Output: Even: 2, Odd: 2Evens are 10 and 20; odds are 15 and 25.
Constraints
0 <= n <= 10^6Elements 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
% 2behave correctly for negative numbers here?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Tally the two parities in a single pass.
- Set
even = 0andodd = 0. - For each element
x:- If
x % 2 == 0, incrementeven. - Otherwise increment
odd.
- If
- 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.
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 →