Practice
Solution
Step 1: Compute xor of all elements
xor = 2 ^ 4 ^ 2 ^ 5 = (2 ^ 2) ^ 4 ^ 5 = 0 ^ 4 ^ 5 = 1 (binary 0001)Step 2: Find rightmost set bit and partition
set_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]
- Mixing order of output
- Incorrect xor calculation
- Miscomputing set bit
Solution
Step 1: Analyze loop condition
The loop runs while right != left, which fails if right < left, causing infinite loop or incorrect behavior.Step 2: Correct condition
Changing condition to 'right > left' ensures loop terminates when right reaches or goes below left.Final Answer:
Option D -> Option DQuick Check:
Loop condition must prevent infinite loops and handle equality correctly [OK]
- Using '!=' instead of '>' causes infinite loops
- Misunderstanding bit-clearing operation
- Returning wrong variable
Solution
Step 1: Identify loops in the XOR approach
There are two separate loops: one over range 0 to n, and one over the array of length n.Step 2: Calculate total time complexity
Each loop runs O(n) times, so total time is O(n) + O(n) = O(n).Final Answer:
Option D -> Option DQuick Check:
No nested loops or sorting, linear time confirmed [OK]
- Confusing XOR operations as nested loops
- Assuming sorting inside XOR
- Ignoring linear iteration over range
Solution
Step 1: Analyze the loop iterations
Each iteration divides n by 4, so the number of iterations is proportional to log base 4 of n.Step 2: Express complexity in Big-O notation
Since log base 4 of n is O(log n), the time complexity is O(log n).Final Answer:
Option D -> Option DQuick Check:
Loop runs ~log4(n) times, which is O(log n) [OK]
- Confusing division steps as O(1)
- Mistaking log log n complexity
Solution
Step 1: Analyze carry calculation
Carry must be (a & b) << 1 to shift carry bits to correct position.Step 2: Identify missing left shift causes infinite recursion
Without shifting, carry never reduces to zero, causing infinite recursion or wrong sum.Final Answer:
Option D -> Option DQuick Check:
Carry missing shift breaks carry propagation [OK]
- Forgetting carry shift
- Incorrect base case
- Not masking intermediate results
