0

I'm having one binary string and have to calculate the number of zeros in that string

i know we can solve it with linear time but i want solution with o(k)

where k is number of zeros in string.

N be the length of String 1<=N<10^6

Abhijeet Gulve
  • 799
  • 1
  • 9
  • 23

2 Answers2

4

Given a string of N independent bits, you will have to look at each bit at least once. This gives a time complexity of O(N). There is no way to do it asymptotically faster.

The algorithms given in the mentioned questions assume, that the whole bit string fits into a word and can be handled in O(1). This does not hold, if the string can be arbitrarily long.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

You can use Hamming weight algorithm, actually calculate hamming distance of the string then subtract from the size of your binary string (N)

Arash Etemad
  • 1,827
  • 1
  • 13
  • 29