Count Odd Numbers in an Interval Range (LeetCode 1523): Solution and Explanation

Easy
Math

Count the odd integers between low and high, both ends included. With high up to 10^9 a loop is too slow, but a prefix count over pairs of numbers gives the answer in two divisions.

Intuition

Walking from low to high and testing each number works, but the range can hold a billion numbers. The count only depends on the two endpoints, so there should be a formula.

Look at 0..x in pairs: (0, 1), (2, 3), (4, 5), and so on. Every complete pair has exactly one odd number. There are x + 1 numbers, so there are (x + 1) // 2 complete pairs, and if x + 1 is odd the leftover single number is x itself, which is even. Either way [0, x] holds (x + 1) // 2 odd numbers.

Counting a range [low, high] is then a prefix difference: odds in [0, high] minus odds in [0, low - 1]. The second count is (low - 1 + 1) // 2 = low // 2, which also works for low = 0.

Approach: Prefix count difference

  1. Compute upto_high = (high + 1) // 2, the odd numbers in [0, high].
  2. Compute below_low = low // 2, the odd numbers in [0, low - 1].
  3. Return upto_high - below_low.

Step-by-step example

low = 3, high = 7
  1. Pairs in 0..7: (0,1), (2,3), (4,5), (6,7). Each has one odd: 1, 3, 5, 7.
  2. upto_high = (7 + 1) // 2 = 4.
  3. Below low, 0..2 holds the odd number 1: below_low = 3 // 2 = 1.
  4. Return 4 - 1 = 3, matching 3, 5 and 7.

Code: Python, Java, C++

1class Solution:
2    def countOdds(self, low: int, high: int) -> int:
3        upto_high = (high + 1) // 2
4        below_low = low // 2
5        return upto_high - below_low
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

Two integer divisions and one subtraction.

Space

O(1)

Two integers.

Other approaches

Loop over the range (time O(high - low), space O(1))

Test every number from low to high with x % 2 == 1. Simple and obviously right, but up to 10^9 iterations is too slow.

Length plus an endpoint check (time O(1), space O(1))

Take (high - low) // 2, and add 1 if either endpoint is odd. It is equivalent, though the case analysis is easier to get wrong than the prefix formula.

Common mistakes

  • Using (high - low + 1) // 2. It undercounts when both ends are odd: [3, 7] has 5 numbers and 3 odds, not 2.
  • Reusing the high formula for the lower end, (low + 1) // 2. That counts odds in [0, low], so an odd low gets subtracted away: [3, 7] would give 4 - 2 = 2 instead of 3. The lower prefix stops at low - 1, which is low // 2.
  • Using floating-point division in JavaScript without Math.floor. (7 + 1) / 2 happens to be exact, but (8 + 1) / 2 is 4.5.

Frequently asked questions

Why is the number of odds in [0, x] equal to (x + 1) // 2?

Split 0..x into pairs (0,1), (2,3), and so on. There are (x + 1) // 2 complete pairs, each with one odd number, and any leftover number is x itself, which is even in that case.

Does the formula work when low is 0?

Yes. low // 2 is 0, so the answer is simply the odd numbers in [0, high].

Can (high + 1) overflow?

No. high is at most 10^9, so high + 1 fits comfortably in a 32-bit int.