Start learning free

LESSON 144 OF 348 · LOOPS & GLOBAL VARIABLES

Odd vs Even in Kotlin: 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

    KOTLIN · LOOPS & GLOBAL VARIABLES

    Odd Even in Kotlin, explained

    Problem overview

    The function oddEven receives an IntArray named numbers and returns a Boolean: true when the array holds more odd numbers than even numbers, and false otherwise. A tie counts as false.

    In the example, [1, 2, 3, 4, 5, 6, 7, 8, 9] has five odd numbers (1, 3, 5, 7, 9) and four even numbers (2, 4, 6, 8). Five is more than four, so the answer is true.

    Approach: two counters and a comparison

    The question compares two quantities, so count both. Walk through the array once. For each number, decide whether it is odd or even and increase the matching counter. At the end, compare the counters.

    The test for oddness uses the remainder operator %. An even number leaves remainder 0 when divided by 2, and an odd number does not. Each number is odd or even, so a plain if with an else covers every case.

    Trace [2, 4, 1] to see the counters move. Both start at 0. The number 2 leaves remainder 0, so evenNumbers becomes 1. The number 4 does the same, making it 2. The number 1 leaves remainder 1, so oddNumbers becomes 1. At the end, 1 is not greater than 2, and the function returns false.

    Only the relative size of the two counts matters, so the actual totals can be large or small. The array length is not needed, and neither is a division to find a percentage.

    Step by step

    1. Declare two counters, oddNumbers and evenNumbers, both starting at 0. They are vars because the loop changes them, and Kotlin infers the type Int from the literal 0.
    2. Loop over the array with for (number in numbers), which visits each element in order.
    3. Count the odd ones. Inside the loop, an if checks number % 2 != 0 and runs oddNumbers++ when it is true. The check is "not 0", not "equal to 1", for a reason explained below.
    4. Count the even ones. The else branch runs exactly when the condition is false, that is, when the remainder is 0, and increments evenNumbers.
    5. Return the comparison oddNumbers > evenNumbers. It is already a Boolean, so no extra if is needed.

    The solution in Kotlin

    class Solution {
        fun oddEven(numbers: IntArray): Boolean {
            var oddNumbers = 0
            var evenNumbers = 0
            for (number in numbers) {
                if (number % 2 != 0) {
                    oddNumbers++
                } else {
                    evenNumbers++
                }
            }
            return oddNumbers > evenNumbers
        }
    }
    

    The Kotlin details behind it:

    • The remainder keeps the sign of the left operand. 7 % 2 is 1, but -3 % 2 is -1, not 1. A test written as number % 2 == 1 would treat -3 as not odd and count it as even. Comparing with 0 works for positive and negative numbers alike, because every even number gives exactly 0, and -4 % 2 is 0.
    • Division and remainder go together. Integer division truncates toward zero, and % is the matching remainder, which is why the sign follows the dividend. If you ever need a result that is never negative, Math.floorMod(-3, 2) gives 1.
    • Other ways to test parity. Kotlin also has number % 2 == 0 for evenness, which gives the same split with the branches swapped, and number and 1, which reads the lowest bit and is 1 for every odd number, negative ones included. The remainder test is the clearest for beginners, so the lesson uses it. Whichever test you pick, compare against 0 when you use %.
    • Comparison results are values. oddNumbers > evenNumbers is a Boolean expression. Writing if (oddNumbers > evenNumbers) { return true } else { return false } would do the same thing in more lines.
    • ++ on a var. oddNumbers++ adds 1 to the variable. It works only on a var, not on a val.

    Complexity

    Let n be the length of numbers.

    Time: O(n). Each element gets one remainder check and one increment.

    Space: O(1). The function stores two counters and the loop variable.

    You could also keep a single counter, adding 1 for an odd number and subtracting 1 for an even one, and then test whether it ends above 0. The two-counter version is easier to read.

    Edge cases and common mistakes

    • A tie. [1, 2] has one odd and one even number, so the function returns false. That is why the comparison is strictly greater.
    • An empty array. Both counters stay 0, and 0 is not greater than 0, so the result is false.
    • Negative numbers. [-3, -5, 2] has two odd numbers and one even number, so the result is true. This is where % 2 == 1 goes wrong.
    • Large arrays. The counters are Int values, so they hold any array length Kotlin can create. They can never overflow.
    • Zero. Zero is even. 0 % 2 is 0, so it lands in the else branch.
    • Returning inside the loop. The function would decide on the first element.
    • Using >=. It would return true for ties.

    Testing

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