Start learning free

LESSON 143 OF 347 · LOOPS & GLOBAL VARIABLES

Odd Even in C#: Counting with the Remainder Operator

Practice Read the full explanation

Given an array, return true if there are more odd numbers than even numbers, otherwise return false.

Example:

Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output: true

Explanation:
There are 5 odd numbers in the array: 1, 3, 5, 7, 9
There are 4 even numbers in the array: 2, 4, 6, 8
5 is greater than 4, so our functions should return true
STEP 1 OF 5
    Ctrl + Enter to check

    C# · LOOPS & GLOBAL VARIABLES

    Odd Even in C#, explained

    Problem overview

    OddEven(int[] numbers) returns true when the array holds more odd numbers than even ones, and false otherwise. For [1, 2, 3, 4, 5, 6, 7, 8, 9] there are five odd numbers (1, 3, 5, 7, 9) and four even ones (2, 4, 6, 8). Five is greater than four, so the answer is true.

    A tie returns false, since "more" means strictly more. An array with equal counts, such as [1, 2], gives false.

    Approach

    Two numbers are enough to answer the question: how many odd values there are and how many even ones. Count both in a single pass, then compare the counts once at the end. Every number is either odd or even, so each element increments exactly one of the two counters, which makes an if / else the natural shape.

    The only real question is how to test oddness. A number is even when dividing it by 2 leaves no remainder. C# has the remainder operator % for that: number % 2 is the remainder of dividing number by 2.

    Step by step

    1. Declare both counters at 0. int oddNumbers = 0, evenNumbers = 0; declares two variables in one statement, each with its own starting value.
    2. Loop with foreach. foreach (int number in numbers) visits each element. The positions don't matter, only the values.
    3. Test for odd. Check number % 2 != 0 and, when it is true, do oddNumbers++. The test is "the remainder is not zero", not "the remainder is 1". The reason is explained below.
    4. Count the even numbers in the else. The else branch runs exactly when the condition was false, which means the remainder was 0, so evenNumbers++.
    5. Return the comparison. After the loop, oddNumbers > evenNumbers is already a bool, so the method returns it directly without another if.

    The solution in C#

    class Solution {
        public bool OddEven(int[] numbers) {
            int oddNumbers = 0, evenNumbers = 0;
            foreach (int number in numbers) {
                if (number % 2 != 0) {
                    oddNumbers++;
                } else {
                    evenNumbers++;
                }
            }
            return oddNumbers > evenNumbers;
        }
    }
    

    The remainder of a negative number

    In C#, the result of % has the same sign as the left operand. Positive numbers behave as expected: 3 % 2 is 1. Negative ones don't:

    3 % 2   gives  1
    -3 % 2  gives -1
    -4 % 2  gives  0
    

    So -3 is odd, but its remainder is -1, not 1. A test written as number % 2 == 1 would treat every negative odd number as even. Because the only even remainder is 0, number % 2 != 0 is correct for all integers, positive, negative and zero. Programmers who know Python often trip on this, since Python's % returns a non-negative result for a positive divisor, and C# does not work that way.

    Also note that % is a remainder operator and not a mathematical modulo: for negative values, "remainder" is the accurate word.

    Other details

    • Two declarations in one line. int a = 0, b = 0; is legal when both variables have the same type. Writing them on separate lines is equally fine.
    • Returning a comparison. Writing if (oddNumbers > evenNumbers) { return true; } else { return false; } gives the same result with more lines. The expression oddNumbers > evenNumbers already is a bool.
    • One counter would be enough. You could add 1 for an odd number and subtract 1 for an even one, then return whether the total is positive. Two counters are clearer for a first solution.

    Tracing the example

    For [1, 2, 3, 4], the counters (odd, even) go from (0, 0) to (1, 0) after the 1, then (1, 1) after the 2, then (2, 1) after the 3 and finally (2, 2) after the 4. The comparison 2 > 2 is false, so the method returns false. Following a small array by hand like this is the quickest way to check a counting loop.

    Another way to test oddness is the bitwise AND: (number & 1) != 0. It checks the lowest bit and gives 1 for odd numbers and 0 for even ones, and in C# it is correct for negative numbers too. The remainder form is the more readable one for beginners.

    Complexity

    Time: O(n), with n the array's length. Each element is examined once, and each check is constant time.

    Space: O(1). Two counters and the loop variable, regardless of the array size.

    Edge cases and common mistakes

    • Negative odd numbers. For [-3, -5, 2] the answer is true, since there are two odd numbers and one even. Testing % 2 == 1 counts them as even and gives false.
    • Zero. Zero is even: 0 % 2 is 0, so it lands in the else branch.
    • A tie or an empty array. Both counters are equal (an empty array has 0 and 0), so the result is false.
    • Using >= at the end. That would make ties return true, which the problem does not want.
    • Using = instead of == or !=. In C# an assignment inside the condition does not compile, because an int is not a bool.
    • Declaring the counters inside the loop. They would reset on every pass, and the final comparison would not compile.

    Testing

    • [1, 2, 3, 4, 5, 6, 7, 8, 9] gives true.
    • [1, 2] gives false, because the counts tie.
    • [-3, -5, 2] gives true.
    • [0, 2, 4] gives false.
    • [] gives false.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free