Challenge - 5 Problems
XOR Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of XOR-based two unique elements finder
What is the output of the following C++ code that finds two non-repeating elements in an array using XOR?
DSA C++
#include <iostream> #include <vector> using namespace std; void findTwoUnique(const vector<int>& arr) { int xor_all = 0; for (int num : arr) { xor_all ^= num; } int set_bit = xor_all & (-xor_all); int x = 0, y = 0; for (int num : arr) { if (num & set_bit) x ^= num; else y ^= num; } cout << x << " " << y << endl; } int main() { vector<int> arr = {2, 3, 7, 9, 2, 3}; findTwoUnique(arr); return 0; }
Attempts:
2 left
💡 Hint
Focus on how XOR groups elements based on the rightmost set bit.
✗ Incorrect
The code first XORs all elements to get xor_all = 7 ^ 9. Then it finds the rightmost set bit to separate elements into two groups. XORing each group gives the two unique numbers 7 and 9. The order is determined by which group is processed first, here 7 then 9.
🧠 Conceptual
intermediateWhy XOR helps find two unique elements?
Why does XOR operation help in finding two non-repeating elements in an array where all other elements repeat twice?
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number with itself.
✗ Incorrect
XOR of a number with itself is zero, so pairs cancel out. XORing all elements leaves XOR of the two unique elements only.
🔧 Debug
advancedIdentify the error in this XOR-based two unique elements code
What error does the following code produce when trying to find two unique elements using XOR?
DSA C++
#include <iostream> #include <vector> using namespace std; void findTwoUnique(const vector<int>& arr) { int xor_all = 0; for (int num : arr) { xor_all ^= num; } int set_bit = xor_all & (xor_all - 1); int x = 0, y = 0; for (int num : arr) { if (num & set_bit) x ^= num; else y ^= num; } cout << x << " " << y << endl; } int main() { vector<int> arr = {4, 1, 2, 1, 2, 5}; findTwoUnique(arr); return 0; }
Attempts:
2 left
💡 Hint
Check how set_bit is calculated and what it represents.
✗ Incorrect
The code uses 'xor_all & (xor_all - 1)' which clears the rightmost set bit instead of isolating it. This causes incorrect grouping and wrong output.
🚀 Application
advancedNumber of steps to find two unique elements using XOR
Given an array of size n with exactly two unique elements and all others repeated twice, how many XOR operations are performed in the standard XOR-based approach?
Attempts:
2 left
💡 Hint
Count XORs in the first loop and the second loop separately.
✗ Incorrect
First loop XORs all n elements once (n XORs). Second loop XORs all n elements again to separate groups (n XORs). Total XORs = 2n.
❓ Predict Output
expertOutput of XOR-based two unique elements finder with negative numbers
What is the output of this C++ code that finds two unique elements in an array containing negative numbers using XOR?
DSA C++
#include <iostream> #include <vector> using namespace std; void findTwoUnique(const vector<int>& arr) { int xor_all = 0; for (int num : arr) { xor_all ^= num; } int set_bit = xor_all & (-xor_all); int x = 0, y = 0; for (int num : arr) { if (num & set_bit) x ^= num; else y ^= num; } cout << x << " " << y << endl; } int main() { vector<int> arr = {-1, -2, -1, -3, -2, -4}; findTwoUnique(arr); return 0; }
Attempts:
2 left
💡 Hint
Negative numbers work the same with XOR; focus on the rightmost set bit.
✗ Incorrect
The XOR of all elements isolates the XOR of -3 and -4. The rightmost set bit separates them correctly, resulting in output '-3 -4'.
