Count Even and Odd Numbers
Given an array of integers, count how many elements are even and how many are odd. An integer is even if dividing it by 2 leaves no remainder, and odd…
Given an array of integers, count how many elements are even and how many are odd. An integer is even if dividing it by 2 leaves no remainder, and odd otherwise. Zero is treated as even. Return `[evenCount, oddCount]`, with the even tally first. You must examine every element exactly once — the array may contain negative numbers, zero, or any mix of values.
- `1 <= n <= 10^5` - `-10^6 <= arr[i] <= 10^6` - The interactive visualizer accepts 1 to 12 integers; longer valid arrays are execution-only. - Elements may be negative, zero, or positive. - Zero is even by definition. - Return `[evenCount, oddCount]`, whose two non-negative values sum to arr.length.
Example 1: input arr = [1, 2, 3, 4, 5, 6]; output [3, 3]. Elements 2, 4, and 6 have zero remainder when divided by 2 (evenCount = 3). Elements 1, 3, and 5 do not (oddCount = 3).
Example 2: input arr = [2, 4, 6]; output [3, 0]. All three elements are divisible by 2. oddCount stays at its initial value of 0 throughout the pass.
Example 3: input arr = [0, -2, -3]; output [2, 1]. 0 and -2 are both even (0 % 2 == 0 and -2 % 2 == 0). -3 is odd. evenCount = 2, oddCount = 1.
Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: linear-scan.