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 🚀