I don't think that there is a shortcut, if the array contains
arbitrary data.
You'll want to create an array of bit counts for either 8 or 16 bits.
I would go with 16.

int bitCount[65536];

// Do this once at initialization time
void init()
{
   bitCount[0] = 0;
   for(int i = 1; i < 65536; ++i) bitCount[i] = bitCount[i/2] + (i&1);
}

// Return the number of bits set in a[n]
int countBits(unsigned int *a, int n)
{
   int result = 0;
   short *p = (short *)a;
   short *q = p + 2*n;
   while(p < q)
      result += bitCount[*(p++)];
   return result;
}

On May 11, 3:35 pm, rahul sharma <rahul23111...@gmail.com> wrote:
> I was searching for google questions and got this question.Use look up to
> do it in bext way
>
> What is best time complexity for this..
> plz post algo too

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.


Reply via email to