Count Odd Numbers in an Interval Range - Problem

Given two non-negative integers low and high, return the count of odd numbers between low and high (inclusive).

An odd number is any integer that is not divisible by 2. For example, 1, 3, 5, 7, 9 are odd numbers.

Example: If low = 3 and high = 7, the numbers in range are [3, 4, 5, 6, 7]. The odd numbers are 3, 5, 7, so the answer is 3.

Input & Output

Example 1 — Basic Range
$ Input: low = 3, high = 7
Output: 3
💡 Note: Numbers in range [3,4,5,6,7]. Odd numbers are 3, 5, 7, so count is 3.
Example 2 — Even Range
$ Input: low = 8, high = 10
Output: 1
💡 Note: Numbers in range [8,9,10]. Only 9 is odd, so count is 1.
Example 3 — Single Number
$ Input: low = 1, high = 1
Output: 1
💡 Note: Range contains only 1, which is odd, so count is 1.

Constraints

  • 0 ≤ low ≤ high ≤ 109

Visualization

Tap to expand
Count Odd Numbers: Range [3, 7]Input:3to7Numbers:34567ODDEVENODDEVENODDOdd Count:3Green boxes (3, 5, 7) are odd numbers
Understanding the Visualization
1
Input Range
Given low=3, high=7
2
Identify Odds
Find odd numbers in [3,4,5,6,7]
3
Count Result
Return count of odd numbers
Key Takeaway
🎯 Key Insight: Use mathematical formula (high+1)/2 - low/2 for instant calculation instead of iteration
Asked in
Microsoft 25 Apple 20 Google 15
180.0K Views
Medium Frequency
~5 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen