LeetCode Challenge Day 83 โ 1523. Count Odd Numbers in an Interval Range
Nitin Ahirwal / December 7, 2025
Hey folks ๐
This is Day 83 of my LeetCode streak ๐
Todayโs problem is 1523. Count Odd Numbers in an Interval Range โ a neat problem that rewards spotting a simple pattern instead of brute force.
๐ Problem Statement
You are given two non-negative integers low and high.
Your task is to return the count of odd numbers between low and high (inclusive).
๐ก Intuition
At first glance, it might seem natural to loop through the range and count odd numbers.
However, since high can be as large as 10โน, iterating would be inefficient.
The key observation is:
- Odd numbers appear at regular intervals
- Roughly half of the numbers in any range are odd
So instead of checking every number, we can compute the answer directly using math.
๐ Approach
- The number of odd numbers from
0tox(inclusive) is:floor((x + 1) / 2) - Count odd numbers from
0tohigh - Count odd numbers from
0tolow - 1 - Subtract the two values to get the count of odd numbers in
[low, high]
This gives us a constant-time solution without any loops.
โฑ๏ธ Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
๐งโ๐ป Code (JavaScript)
/**
* @param {number} low
* @param {number} high
* @return {number}
*/
var countOdds = function(low, high) {
return Math.floor((high + 1) / 2) - Math.floor(low / 2);
};
๐งฉ Example Walkthrough
Input:
low = 3, high = 7
Odd numbers: [3, 5, 7]
Output: 3
Input:
low = 8, high = 10
Odd numbers: [9]
Output: 1
๐ฏ Reflection
This problem is a great reminder that:
-
Not every problem needs loops
-
Simple math observations can drastically simplify a solution
-
Thinking in terms of patterns often beats brute force
Thatโs it for Day 83 of my LeetCode challenge ๐ช
See you tomorrow ๐