The final XOR after processing all elements is 14.
Final Answer:
14 -> Option D
Quick Check:
Stepwise XOR = 14 [OK]
Hint: XOR all elements stepwise to find combined XOR [OK]
Common Mistakes:
Forgetting XOR is cumulative and order matters
Mistaking XOR with addition
Stopping XOR early
4. Identify the error in the following code snippet that attempts to find two non-repeating elements using XOR:
arr = [4, 1, 2, 1, 2, 5] xor = 0 for num in arr: xor ^= num set_bit = xor & ~(xor - 1) num1 = 0 num2 = 0 for num in arr: if (num & set_bit) > 0: num1 ^= num else: num2 ^= num print(num1, num2)
medium
A. No error, code works correctly
B. XOR initialization should be arr[0] instead of 0
C. Incorrect calculation of set_bit using bitwise NOT
Elements are split by set_bit and XORed separately to find unique elements.
Step 3: Confirm output correctness
Code correctly prints the two unique numbers without errors.
Final Answer:
No error, code works correctly -> Option A
Quick Check:
Correct set_bit and partitioning = code works [OK]
Hint: Use xor & ~(xor-1) to find rightmost set bit correctly [OK]
Common Mistakes:
Misusing bitwise NOT causing wrong set_bit
Initializing XOR incorrectly
Using wrong condition in if statement
5. Given an array arr = [10, 20, 10, 30, 40, 20], which of the following Python code snippets correctly finds the two non-repeating elements using XOR and prints them in ascending order?
hard
A. xor = 0
for num in arr:
xor ^= num
set_bit = xor & (-xor)
num1 = 0
num2 = 0
for num in arr:
if num & set_bit:
num1 ^= num
else:
num2 ^= num
print(sorted([num1, num2]))
B. xor = 0
for num in arr:
xor ^= num
set_bit = xor | (xor - 1)
num1 = 0
num2 = 0
for num in arr:
if num & set_bit:
num1 ^= num
else:
num2 ^= num
print(sorted([num1, num2]))
C. xor = arr[0]
for num in arr[1:]:
xor ^= num
set_bit = xor & ~(xor - 1)
num1 = 0
num2 = 0
for num in arr:
if (num & set_bit) == 0:
num1 ^= num
else:
num2 ^= num
print(sorted([num1, num2]))
D. xor = 0
for num in arr:
xor ^= num
set_bit = xor & ~(xor + 1)
num1 = 0
num2 = 0
for num in arr:
if (num & set_bit) != 0:
num1 ^= num
else:
num2 ^= num
print(sorted([num1, num2]))
Solution
Step 1: Calculate XOR of all elements
xor accumulates XOR of all elements, isolating XOR of two unique numbers.
Step 2: Find rightmost set bit correctly
set_bit = xor & (-xor) correctly isolates the rightmost set bit for partitioning.
Step 3: Partition elements and XOR separately
Elements are split by set_bit and XORed to find the two unique numbers.
Step 4: Sort and print unique elements
Sorting ensures output is in ascending order as required.
Final Answer:
Code in option A correctly finds and prints [30, 40] -> Option A
Quick Check:
Rightmost set bit with xor & (-xor) + partition = correct [OK]
Hint: Use xor & (-xor) to find set bit and partition array [OK]
Common Mistakes:
Using incorrect set_bit calculation like xor | (xor - 1)
Wrong initialization of xor variable
Incorrect condition in partition if statement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.