LeetCode Entry

1523. Count Odd Numbers in an Interval Range

07.12.2025 easy 2025 kotlin rust

Odds l..h

1523. Count Odd Numbers in an Interval Range easy blog post substack youtube

ff0763dc-acf5-46ba-a96b-cd897810f634 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1196

Problem TLDR

Odds l..h #easy

Intuition

Look at total numbers count d:

  • even d - just divide by 2
  • odd d - divide by 2 plus look at first number if it is odd
    // 1 2      2-1+1=2,  l=o, h = e, 2/2
    // 1 2 3 4  4, l=o h=e 4/2
    // 1 2 3    3   l=o h=o   1+3/2
    // 2 3      2   l=e    2/2
    // 2 3 4 5  4   l=e    4/2
    // 3 4 5    3   l=o    1+3/2
    // 2 3 4    3   l=e    3/2
    // 3 4 5 6  4   l=o    4/2

Approach

  • or brillian lee compression of this logic: (h+1)/2-l/2

Complexity

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

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

Code

// 76ms
    fun countOdds(l: Int, h: Int) =
        (h-l+1)/2 + (l%2)*((h-l+1)%2)
// 0ms
    pub fn count_odds(l: i32, h: i32) -> i32 {
       (h+1)/2 - l/2
    }