Bit flipping algorithm implementation or psuedo-code

87 Views Asked by At

I am trying to understand and implement Bit flipping algorithm

Can someone share the psuedocode for the algorithm and explain in detail? I cannot understand the answer.

I also want to understand what topic this question falls under (like Dynamic Programming or Optimization).

1

There are 1 best solutions below

0
On BEST ANSWER

The answer was saying to build up the string of all $1$ incrementally from the beginning: to have first bits $1$, then to have first two bits $1$, ..., then to have the first $n-k+1$ bits all in $1$.

The algorithm is that if the first $i-1$ bits are all $1$ and the $i$th bit is $0$ (where $1 \le i \le n-k+1$), then flip the bits from $i$ to $i+k-1$ inclusive will make at least the first $i$ bits all $1$.