Bird
Raised Fist0

Given the following code snippet using XOR partitioning to find two missing numbers, what is the output for n=6 and arr=[1, 2, 4, 6]?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - Find Two Missing Numbers
Given the following code snippet using XOR partitioning to find two missing numbers, what is the output for n=6 and arr=[1, 2, 4, 6]?
A[3, 5]
B[2, 5]
C[3, 6]
D[4, 5]
Step-by-Step Solution
Solution:
  1. Step 1: Compute xor_all

    XOR 1 to 6 = 1^2^3^4^5^6 = 7; XOR arr = 1^2^4^6 = 7; xor_all = 7 ^ 7 = 0; Actually, let's compute carefully: 1^2=3, 3^3=0, 0^4=4, 4^5=1, 1^6=7; arr XOR: 1^2=3, 3^4=7, 7^6=1; xor_all = 7 ^ 1 = 6.
  2. Step 2: Find rightmost set bit and partition

    set_bit = 6 & (-6) = 2; Partition numbers and arr by bit 2; XOR partitions yield missing numbers 3 and 5.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Output matches expected missing numbers [OK]
Quick Trick: XOR partitions isolate missing numbers by set bit [OK]
Common Mistakes:
MISTAKES
  • Incorrect XOR calculation
  • Wrong set bit extraction
  • Mixing partitions
Trap Explanation:
PITFALL
  • Candidates often miscalculate xor_all or set_bit, leading to wrong missing numbers.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to trace XOR partitioning code
Master "Find Two Missing Numbers" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes