Count Odd Numbers in an Interval Range — LeetCode 1523 Python Solution
EasyMath
- Problem
- #1523
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
Example
- Input
- low = 3, high = 7
- Output
- 3
- Explanation
- The odd numbers between 3 and 7 are [3,5,7].
Python solution
Python
class Solution:
def countOdds(self, low: int, high: int) -> int:
return ((high + 1) >> 1) - (low >> 1)Complexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1523. Count Odd Numbers in an Interval Range is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1523. Count Odd Numbers in an Interval Range?
- LeetCode 1523. Count Odd Numbers in an Interval Range is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1523. Count Odd Numbers in an Interval Range?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 1523. Count Odd Numbers in an Interval Range?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1523. Count Odd Numbers in an Interval Range cover?
- LeetCode 1523. Count Odd Numbers in an Interval Range is tagged Math on LeetCode.