Skip to content
Discussion options

You must be logged in to vote

We are given two non-negative integers low and high. We need to count the number of odd numbers between low and high (inclusive).

The straightforward way is to iterate from low to high and count the odd numbers. However, this would be O(n) and might be too slow for large inputs (up to 10⁹). Therefore, we need a mathematical approach.

Approach:

  • Calculate the number of odd numbers from 0 to high using integer division: (high + 1) // 2
  • Calculate the number of odd numbers from 0 to low - 1 using integer division: low // 2
  • Subtract the second result from the first to get the count of odd numbers in the range [low, high]

Let's implement this solution in PHP: 1523. Count Odd Numbers in an Inte…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Dec 7, 2025
Maintainer Author

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Dec 7, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 7, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants