Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
Why is the big O runtime of 1D parity checking log(n)?

So I am reading some code from this website: http://www.geeksforgeeks.org/write-a-c-program-to-find-the-parity-of-an-unsigned-integer/

And it shows how to determine whether a number has even or odd parity. However, I don't understand why the runtime efficiency is log(n). Here is the code for reference:

# include <stdio.h>
# define  bool int

/* Function to get parity of number n. It returns 1
   if n has odd parity, and returns 0 if n has even
   parity */
bool getParity(unsigned int n)
{
    bool parity = 0;
    while (n)
    {
        parity = !parity;
        n      = n & (n - 1);
    }        
    return parity;
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The runtime efficiency is O(log(n)), where n is the value of the integer you pass in. However, that's an unconventional way to use O notation.

More frequently, O notation is expressed in terms of the size of the input in bits (the # of bits needed to represent the input), in which case the size of the input is k=O(log2(n)) and the runtime is O(k).

(Even more accurately, the runtime is Θ(s) where s is the number of bits set in n, although that assumes bit operations are O(1)).

over 4 years ago · Santiago Trujillo Relatório

0

See this So question here

As you see we count no of 1's using this the loop will run exactly the no of bits that are one(1)(let say p) in the binary representation of n.

Thus complexity is Θ(p).

and as the maximum no of bits used to represent n is log2(n) , thus upper asymptotic bound id O(log2(n)).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda