Bit Manipulation - Number of 1 Bits (Hamming Weight)What is the time complexity of Brian Kernighan's algorithm for counting set bits in a 32-bit integer?AO(n log n) due to bitwise operationsBO(32) because it checks all bits one by oneCO(1) constant time since input size is fixedDO(k) where k is the number of set bits in the integerCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand algorithm stepsEach iteration clears one set bit, so loop runs k times where k is number of set bits.Step 2: Analyze complexityTime complexity depends on k, not total bits, so O(k).Final Answer:Option D -> Option DQuick Check:Loop runs once per set bit, not all bits [OK]Quick Trick: Time depends on set bits count, not total bits [OK]Common Mistakes:MISTAKESAssuming always O(32) ignoring set bitsConfusing bitwise ops with log factorsTrap Explanation:PITFALLMany think it always scans all bits, missing that it skips zero bits quickly.Interviewer Note:CONTEXTTests understanding of bit manipulation time complexity nuances.
Master "Number of 1 Bits (Hamming Weight)" in Bit Manipulation3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Bit Manipulation Quizzes Bitwise AND of Numbers Range - Bitwise AND of Numbers Range - Quiz 15hard Find Two Missing Numbers - Find Two Missing Numbers - Quiz 4medium Find Two Missing Numbers - Find Two Missing Numbers - Quiz 7medium Generate All Permutations Using Bitmask DP - Generate All Permutations Using Bitmask DP - Quiz 9hard Gray Code - Gray Code - Quiz 13medium Maximum XOR of Two Numbers - Maximum XOR of Two Numbers - Quiz 5medium Power of Four - Power of Four - Quiz 5medium Power of Two - Power of Two - Quiz 7medium Reverse Bits - Reverse Bits - Quiz 15hard UTF-8 Validation - UTF-8 Validation - Quiz 13medium