1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Optimized approach for Counting odd Numbers in an interval range [low,high] in python...

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
    I solved it multiple times using loops but those methods were not optimal enough to get my answer submitted is there much better alternative?

    So The Thing is we can easily solve it using loops but is there any much more optimal way to do this faster ?

    class solution:
    def countOdds(self,low:int,high:int) -> int:
    count = 0
    for i in range (low,high-1):
    if i % 2 != 0:
    count+=1
    return count


    This was the most optimal answer that i was able to get is there much more optimal answer for this ?
    Specifically in Python.

    Continue reading...

Compartilhe esta Página