Given two non-negative integers low and high, return the count of odd numbers between low and high (inclusive).
-
Example:
low = 3,high = 7→ odd numbers are[3, 5, 7], so the answer is3.
We need to compute this without iterating over the whole range (because the range can be huge).
0 <= low <= high <= 10^9
These constraints tell us:
- The range length can be very large, up to
10^9numbers. - A simple loop from
lowtohighwill be too slow. - We must use O(1) math-based logic.
I thought like this:
- Between any two consecutive integers, one is odd and the other is even.
- So, in a long sequence of integers, almost half of them are odd.
- Instead of manually checking each number, I tried to find a formula to directly count the odd numbers.
Then I realized:
- Count of odd numbers from
1toxis:
[ \text{odds up to } x = \left\lfloor \frac{x + 1}{2} \right\rfloor ]
So if I can compute:
- odds from
1tohigh, and - odds from
1tolow - 1
Then:
[ \text{odds in } [low, high] = \text{odds up to high} - \text{odds up to (low - 1)} ]
This gives a clean O(1) solution.
-
Define a helper idea:
oddsUpTo(x) = (x + 1) // 2- This returns how many odd numbers exist from
1tox.
-
Use inclusion-exclusion style logic:
- Odds in
[1, high]→oddsUpTo(high) - Odds in
[1, low-1]→oddsUpTo(low - 1) - Odds in
[low, high]=oddsUpTo(high) - oddsUpTo(low - 1)
- Odds in
-
Implement this in each language using integer division:
- C++ / Java / Go:
/automatically floors for integers. - Python: use
//. - JavaScript: use
Math.floor(...)because/is floating point.
- C++ / Java / Go:
-
Return the final difference as the answer.
No loops, no arrays, just math.
-
Only primitive integers:
low,high, maybe a localx.
-
No arrays, no lists, no maps, nothing extra.
So the space usage is constant.
-
Input: two integers
lowandhigh. -
Operation:
- Compute
oddsUpTo(high). - Compute
oddsUpTo(low - 1). - Subtract the second from the first.
- Compute
-
Output: a single integer representing how many odd numbers exist in the interval
[low, high].
Behavior:
-
Works correctly for:
low == highlow == 0- Very large ranges up to
10^9.
-
Time Complexity:
O(1)We do a constant number of arithmetic operations, no loops, no recursion. -
Space Complexity:
O(1)Only a few integer variables are used. No extra data structure grows with input size.
class Solution {
public:
int countOdds(int low, int high) {
// Helper lambda: count of odd numbers from 1 to x
auto oddsUpTo = [](int x) -> int {
// Integer division automatically floors the result
return (x + 1) / 2;
};
// Odds in [low, high] = oddsUpTo(high) - oddsUpTo(low - 1)
return oddsUpTo(high) - oddsUpTo(low - 1);
}
};class Solution {
public int countOdds(int low, int high) {
// Odds in [low, high] = oddsUpTo(high) - oddsUpTo(low - 1)
return oddsUpTo(high) - oddsUpTo(low - 1);
}
// Helper: count odd numbers from 1 to x
private int oddsUpTo(int x) {
return (x + 1) / 2; // integer division floors automatically
}
}/**
* @param {number} low
* @param {number} high
* @return {number}
*/
var countOdds = function(low, high) {
// Helper: count odd numbers from 1 to x
const oddsUpTo = (x) => {
// Use Math.floor because JS division is floating point
return Math.floor((x + 1) / 2);
};
// Odds in [low, high]
return oddsUpTo(high) - oddsUpTo(low - 1);
};class Solution:
def countOdds(self, low: int, high: int) -> int:
# Helper: count odd numbers from 1 to x
def odds_up_to(x: int) -> int:
# // is integer (floor) division
return (x + 1) // 2
# Odds in [low, high]
return odds_up_to(high) - odds_up_to(low - 1)package main
func countOdds(low int, high int) int {
// Helper: count odd numbers from 1 to x
oddsUpTo := func(x int) int {
return (x + 1) / 2
}
// Odds in [low, high]
return oddsUpTo(high) - oddsUpTo(low-1)
}The logic is the same in all languages; only syntax changes. Let’s break it down step by step.
Core idea:
oddsUpTo(x) = floor( (x + 1) / 2 )
Why?
-
If
xis even:- Example:
x = 8 - Numbers: 1,2,3,4,5,6,7,8
- Odds: 1,3,5,7 → 4 odds
(8 + 1) / 2 = 9 / 2 = 4(floor) ✅
- Example:
-
If
xis odd:- Example:
x = 7 - Numbers: 1,2,3,4,5,6,7
- Odds: 1,3,5,7 → 4 odds
(7 + 1) / 2 = 8 / 2 = 4✅
- Example:
So (x + 1) // 2 always gives correct count of odds from 1 to x.
We use the concept of prefix counts.
- Odds from
1tohigh→oddsUpTo(high) - Odds from
1tolow - 1→oddsUpTo(low - 1)
Now, odds in [low, high] are exactly:
oddsUpTo(high) - oddsUpTo(low - 1)
This works because:
- When we subtract odds up to
low - 1, we remove all odds beforelow. - We are left only with odds from low to high.
-
C++ / Java / Go:
(x + 1) / 2
Using integer variables means division automatically floors.
-
Python:
(x + 1) // 2
Here
//is the integer division operator. -
JavaScript:
Math.floor((x + 1) / 2)
Because JS
/returns a floating point number.
In each language, we:
- Implement
oddsUpTo(x)as a small helper (or inline math). - Compute
oddsUpTo(high) - oddsUpTo(low - 1). - Return the result.
That’s the entire solution.
Input:
low = 3, high = 7
Step-by-step:
-
Odds up to 7:
(7 + 1) // 2 = 8 // 2 = 4
-
Odds up to 2 (low - 1 = 2):
(2 + 1) // 2 = 3 // 2 = 1
-
Difference:
4 - 1 = 3
Output:
3
Explanation: odd numbers are [3, 5, 7].
Input:
low = 8, high = 10
Step-by-step:
-
Odds up to 10:
(10 + 1) // 2 = 11 // 2 = 5
-
Odds up to 7 (low - 1 = 7):
(7 + 1) // 2 = 8 // 2 = 4
-
Difference:
5 - 4 = 1
Output:
1
Explanation: odd numbers are [9].
You can copy any language solution into your local environment or into an online judge.
g++ -std=c++17 main.cpp -o main
./mainMake sure main.cpp includes the class Solution and some main() for testing.
javac Solution.java
java SolutionPut the class Solution in Solution.java.
In LeetCode, you don’t need main, just submit the class.
node main.jsCreate main.js, define the function countOdds, and call it with sample values to test.
python3 main.pyCreate main.py with the Solution class and write some test code like:
print(Solution().countOdds(3, 7))go run main.goCreate main.go, define countOdds, and call it inside main().
-
This solution is already optimal:
- O(1) time — just constant arithmetic.
- O(1) space — no extra memory.
-
No loops, no recursion, no data structures.
-
The logic is purely mathematical and works safely under the given constraints.
-
Also avoids overflow:
xis at most10^9, sox + 1is fine in all supported languages.