6. Evens and Odds Tally

A theatre hands out numbered tickets. Tickets with an even number go through the left gate and tickets with an odd number go through the right gate. The manager wants to know how many tickets went through each gate.

Write a method countEvenOdd that takes the array nums of ticket numbers and returns an array of exactly two integers: the count of even numbers first, then the count of odd numbers. Zero counts as even.

Example 1

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

2 and 4 are even, while 1, 3 and 5 are odd, so the answer is [2, 3].

Example 2

Input:
nums = [8,10,12]
Output:
[3,0]
Explanation:

Every ticket is even, so nothing goes through the right gate: [3, 0].

Constraints

1 ≤ nums.length ≤ 1000

-109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…