Difficulty: Easy Tags: Math Link: https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/
Given two integers low and high, return the number of odd integers within the inclusive interval:
[low, high]
Example:
low = 3, high = 7 → odd numbers are [3, 5, 7] → result is 3.
The naive approach—looping from low to high and checking each number—fails for large ranges (up to 10^9).
We need a mathematical O(1) solution.
This computes the answer in constant time, with no loops.
We use:
This counts all odd numbers up to high.
We use:
This automatically excludes low itself, whether it is odd or even.
Final result:
Input: low = 3, high = 7
Odds up to 7: (7 + 1) // 2 = 4 → [1, 3, 5, 7]
Odds up to 2: 3 // 2 = 1 → [1]
4 - 1 = 3
Input: low = 8, high = 10
Odds up to 10: (10 + 1) // 2 = 5 → [1, 3, 5, 7, 9]
Odds up to 7: 8 // 2 = 4 → [1, 3, 5, 7]
5 - 4 = 1
- Odd numbers appear every 2 steps: 1, 3, 5, 7, …
- Integer division naturally counts how many such values exist in a range.
- This converts a potentially huge iteration into a simple formula.
- Runs in O(1) time and O(1) space.
| Operation | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) |
This is the optimal solution.
- How parity-based problems can often be solved using integer patterns instead of loops.
- Why interval problems can be computed using prefix-style counting (
count(0..high) − count(0..low−1)). - How mathematical reasoning avoids time-limit exceed in large input ranges.