LeetCode Entry

1523. Count Odd Numbers in an Interval Range

13.02.2023 easy 2023 kotlin

fun countOdds(low: Int, high: Int): Int {

1523. Count Odd Numbers in an Interval Range easy

blog post

    fun countOdds(low: Int, high: Int): Int {
        if (low == high) return if (low % 2 == 0) 0 else 1
        val lowOdd = low % 2 != 0
        val highOdd = high % 2 != 0
        val count = high - low + 1
        return if (lowOdd && highOdd) {
            1 + count / 2
        } else if (lowOdd || highOdd) {
            1 + (count - 1) / 2
        } else {
            1 + ((count - 2) / 2)
        }
    }

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/117

Intuition

Count how many numbers in between, subtract even on the start and the end, then divide by 2.

Complexity

  • Time complexity: \(O(1)\)
  • Space complexity: \(O(1)\)