Min Bit Flips To Reach Goal In O(log2X) Time
To find bits to flip, use XOR(start, goal). Count 1s in result using log time complexity.
Problem We have to find out how many bits are needed to be flipped in start to make it equal to goal We know that exor produces 1 for odd number of 1 else 0 This can help us in identifying how many bits are needed to be flipped in start to make it equal to goal TC: O(log2X)O(log_{2}X)O(log2X) Where X is (start exor end) SC: O(1) class Solution { public int minBitFlips(int start, int goal) { int exorVal = start ^ goal; StringBuilder str = new StringBuilder(); int count = 0; //logarithmic time complexity while(exorVal!=0){ if(exorVal%2 ==1)...