Bit Manipulation - Single Number III (Two Singles)Given the following code and input, what is the final returned list?A[2, 5]B[5, 4]C[4, 5]D[2, 4]Check Answer
Step-by-Step SolutionStep 1: Compute xor of all elementsxor = 2 ^ 4 ^ 2 ^ 5 = (2 ^ 2) ^ 4 ^ 5 = 0 ^ 4 ^ 5 = 1 (binary 0001)Step 2: Find rightmost set bit and partitionset_bit = xor & (-xor) = 1 & (-1) = 1. Partition nums by bit 1: group1 (bit set): 5, group2 (bit not set): 2,4,2. XOR group1 = 5, XOR group2 = 4.Final Answer:Option C -> Option CQuick Check:Output matches unique elements [OK]Quick Trick: Rightmost set bit partitions unique numbers correctly [OK]Common Mistakes:MISTAKESMixing order of outputIncorrect xor calculationMiscomputing set bitTrap Explanation:PITFALLCandidates often confuse xor or set bit calculation leading to wrong partitioning.Interviewer Note:CONTEXTTests ability to trace bitwise XOR and partitioning logic precisely.
Master "Single Number III (Two Singles)" in Bit Manipulation3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Bit Manipulation Quizzes Find the Missing Number - Find the Missing Number - Quiz 4medium Generate All Permutations Using Bitmask DP - Generate All Permutations Using Bitmask DP - Quiz 6medium Generate All Permutations Using Bitmask DP - Generate All Permutations Using Bitmask DP - Quiz 4medium Gray Code - Gray Code - Quiz 15hard Power of Four - Power of Four - Quiz 11easy Reverse Bits - Reverse Bits - Quiz 10hard Reverse Bits - Reverse Bits - Quiz 11easy Single Number II (Three Copies) - Single Number II (Three Copies) - Quiz 11easy Single Number II (Three Copies) - Single Number II (Three Copies) - Quiz 14medium Subsets Using Bitmask - Subsets Using Bitmask - Quiz 8hard