Back to posts

LeetCode Challenge Day 83 โ€” 1523. Count Odd Numbers in an Interval Range

Nitin Ahirwal / December 7, 2025

LeetCode ChallengeDay 83MathParityJavaScriptEasy

Hey folks ๐Ÿ‘‹

This is Day 83 of my LeetCode streak ๐Ÿš€
Todayโ€™s problem is 1523. Count Odd Numbers in an Interval Range โ€” a neat problem that rewards spotting a simple pattern instead of brute force.


๐Ÿ“Œ Problem Statement

You are given two non-negative integers low and high.

Your task is to return the count of odd numbers between low and high (inclusive).


๐Ÿ’ก Intuition

At first glance, it might seem natural to loop through the range and count odd numbers.
However, since high can be as large as 10โน, iterating would be inefficient.

The key observation is:

  • Odd numbers appear at regular intervals
  • Roughly half of the numbers in any range are odd

So instead of checking every number, we can compute the answer directly using math.


๐Ÿ”‘ Approach

  1. The number of odd numbers from 0 to x (inclusive) is: floor((x + 1) / 2)
  2. Count odd numbers from 0 to high
  3. Count odd numbers from 0 to low - 1
  4. Subtract the two values to get the count of odd numbers in [low, high]

This gives us a constant-time solution without any loops.


โฑ๏ธ Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

๐Ÿง‘โ€๐Ÿ’ป Code (JavaScript)

/**
 * @param {number} low
 * @param {number} high
 * @return {number}
 */
var countOdds = function(low, high) {
    return Math.floor((high + 1) / 2) - Math.floor(low / 2);
};

๐Ÿงฉ Example Walkthrough

Input:
low = 3, high = 7

Odd numbers: [3, 5, 7]
Output: 3


Input:
low = 8, high = 10

Odd numbers: [9]
Output: 1


๐ŸŽฏ Reflection

This problem is a great reminder that:

  • Not every problem needs loops

  • Simple math observations can drastically simplify a solution

  • Thinking in terms of patterns often beats brute force

Thatโ€™s it for Day 83 of my LeetCode challenge ๐Ÿ’ช
See you tomorrow ๐Ÿš€