Classic problem #1523EasyMath
Count Odd Numbers in an Interval Range
Real-world version: Odd Numbers Inside a RangeTracked in this browser only
Read Count Odd Numbers in an Interval Range on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
Return how many odd numbers lie in the range from low to high, counting both ends.
Examples
Example 1
- Input
- low = 1, high = 10
- Output
- 5
The odd numbers from 1 to 10 are 1, 3, 5, 7 and 9.
Example 2
- Input
- low = 2, high = 2
- Output
- 0
The range holds a single number and it is even.
Example 3
- Input
- low = 5, high = 5
- Output
- 1
The range holds a single number and it is odd.
Constraints
- 0 <= low <= high <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def count_odds(low: int, high: int) -> int:Java
public int countOdds(int low, int high)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's