Here i have a binary string, for example - "01010011"
. The positions of set bits are = 0, 1, 4, 6
(from right to left). I have to do series of operations something like this.
for binary string - 01010011
unset the 0th set bit. - 01010010 (new set bit positions - 1, 4, 6)
unset the 0th set bit - 01010000 (new set bit positions - 4, 6)
unset the 1st set bit - 00010000 (new set bit positions - 4)
As you can see after each operation my binary string changes and the new operations should be done on that.
My approach was to make a copy of binary string and loop through k-1 times and unset the rightmost set bit. after k-1 loop, my rightmost set bit will be the actual kth bit and i can get the position of this and unset this position in my original binary. But this method looks very inefficient to me.
I need some efficient approaches and c/c++(bitset) or python code is highly appreciated.
Note:
The kth bit will be always set in my binary string