Program to count odd numbers in an interval range using Python

Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive).

So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3, 5, 7, 9, 11, 13, 15], there are 7 elements.

Algorithm

To solve this, we will follow these steps:

  • If left is odd or right is odd, then

    • return 1 + quotient of (right-left) / 2

  • Otherwise,

    • return quotient of (right-left) / 2

Method 1: Using Mathematical Formula

Let us see the following implementation to get better understanding:

def count_odd_numbers(left, right):
    if left % 2 == 1 or right % 2 == 1:
        return (right - left) // 2 + 1
    else:
        return (right - left) // 2

left = 3
right = 15
result = count_odd_numbers(left, right)
print(f"Odd numbers between {left} and {right}: {result}")
Odd numbers between 3 and 15: 7

Method 2: Using Loop and Counting

An alternative approach is to iterate through the range and count odd numbers:

def count_odd_numbers_loop(left, right):
    count = 0
    for num in range(left, right + 1):
        if num % 2 == 1:
            count += 1
    return count

left = 3
right = 15
result = count_odd_numbers_loop(left, right)
print(f"Odd numbers between {left} and {right}: {result}")

# Let's also display the actual odd numbers
odd_numbers = [num for num in range(left, right + 1) if num % 2 == 1]
print(f"Actual odd numbers: {odd_numbers}")
Odd numbers between 3 and 15: 7
Actual odd numbers: [3, 5, 7, 9, 11, 13, 15]

How It Works

The mathematical formula works because:

  • If either boundary is odd, we're guaranteed to include at least one odd number

  • The difference (right - left) divided by 2 gives us the number of pairs

  • Each pair contains exactly one odd and one even number

Testing with Different Examples

def count_odd_numbers(left, right):
    if left % 2 == 1 or right % 2 == 1:
        return (right - left) // 2 + 1
    else:
        return (right - left) // 2

# Test cases
test_cases = [(3, 15), (2, 8), (1, 10), (4, 6)]

for left, right in test_cases:
    result = count_odd_numbers(left, right)
    odd_nums = [num for num in range(left, right + 1) if num % 2 == 1]
    print(f"Range [{left}, {right}]: {result} odd numbers - {odd_nums}")
Range [3, 15]: 7 odd numbers - [3, 5, 7, 9, 11, 13, 15]
Range [2, 8]: 3 odd numbers - [3, 5, 7]
Range [1, 10]: 5 odd numbers - [1, 3, 5, 7, 9]
Range [4, 6]: 1 odd numbers - [5]

Comparison

Method Time Complexity Space Complexity Best For
Mathematical Formula O(1) O(1) Large ranges, efficiency
Loop and Count O(n) O(1) Small ranges, verification

Conclusion

The mathematical formula approach is more efficient with O(1) time complexity. Use the loop method for verification or when you need to see the actual odd numbers in the range.

Updated on: 2026-03-25T20:18:08+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements