File: count-odd-numbers-in-an-interval-range/solution.py

Date: 2026-06-06

Time: 16:02

Purpose

This file solves LeetCode 1523 — Count Odd Numbers in an Interval Range. It provides a single function count_odds that computes the count of odd integers in a closed interval [low, high] using O(1) arithmetic rather than iteration.

Key Components

count_odds(low: int, high: int) -> int

The only function. It returns the number of odd integers in [low, high] inclusive.

The formula (high + 1) // 2 - low // 2 works by leveraging a counting identity:

Subtracting yields the count in [low, high].

Walk-through with examples:

| low | high | (high+1)//2 | low//2 | result | odds in range |

|-----|------|---------------|----------|--------|---------------|

| 3 | 7 | 4 | 1 | 3 | {3, 5, 7} |

| 2 | 8 | 4 | 1 | 3 | {3, 5, 7} |

| 1 | 1 | 1 | 0 | 1 | {1} |

| 2 | 2 | 1 | 1 | 0 | {} |

Patterns

Dependencies

Imports: None — pure arithmetic, no standard library or project imports.

Imported by: The testsolution.py in the same directory. The "Imported By" list in the prompt is misleading — those are unrelated test files that happen to share a common test harness import pattern, not actual importers of countodds.

Flow

Straight-line: one expression, one return. No branching, no loops, no mutation.

Invariants

Error Handling

None. The function is a pure arithmetic expression with no failure modes for valid inputs. No exceptions are raised or caught.

Topics to Explore

Beliefs