Challenge - 5 Problems
XOR Mastery Badge
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 this Rust code that finds two unique numbers in an array where every other number repeats twice?
DSA Rust
fn find_two_unique(nums: &[i32]) -> (i32, i32) {
let xor_all = nums.iter().fold(0, |acc, &x| acc ^ x);
let rightmost_set_bit = xor_all & (-xor_all);
let (mut x, mut y) = (0, 0);
for &num in nums {
if num & rightmost_set_bit != 0 {
x ^= num;
} else {
y ^= num;
}
}
(x, y)
}
fn main() {
let arr = [2, 4, 7, 9, 2, 4];
let result = find_two_unique(&arr);
println!("({}, {})", result.0, result.1);
}Attempts:
2 left
💡 Hint
Focus on how XOR cancels out duplicate numbers and how the rightmost set bit helps separate the two unique numbers.
✗ Incorrect
The XOR of all numbers gives xor_all = 7 ^ 9. The rightmost set bit in xor_all helps split numbers into two groups. XORing each group separately gives the two unique numbers 7 and 9. The order is (7, 9) because x accumulates numbers with the set bit.
🧠 Conceptual
intermediateWhy XOR helps find two unique elements?
Why does XOR operation help in finding two unique elements in an array where every other element repeats 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 all duplicates cancel out. The remaining XOR is the XOR of the two unique numbers.
🔧 Debug
advancedIdentify the error in this Rust code for two unique elements
What error does this Rust code produce when trying to find two unique elements using XOR?
DSA Rust
fn find_two_unique(nums: &[i32]) -> (i32, i32) {
let xor_all = nums.iter().fold(0, |acc, &x| acc ^ x);
let rightmost_set_bit = xor_all & (xor_all - 1);
let (mut x, mut y) = (0, 0);
for &num in nums {
if num & rightmost_set_bit != 0 {
x ^= num;
} else {
y ^= num;
}
}
(x, y)
}
fn main() {
let arr = [1, 2, 1, 3, 2, 5];
let result = find_two_unique(&arr);
println!("({}, {})", result.0, result.1);
}Attempts:
2 left
💡 Hint
Check how rightmost_set_bit is calculated and what it should represent.
✗ Incorrect
The code uses xor_all & (xor_all - 1) which clears the rightmost set bit instead of isolating it. This causes wrong grouping and incorrect output.
❓ Predict Output
advancedOutput of XOR grouping with negative numbers
What is the output of this Rust code that finds two unique numbers including a negative number?
DSA Rust
fn find_two_unique(nums: &[i32]) -> (i32, i32) {
let xor_all = nums.iter().fold(0, |acc, &x| acc ^ x);
let rightmost_set_bit = xor_all & (-xor_all);
let (mut x, mut y) = (0, 0);
for &num in nums {
if num & rightmost_set_bit != 0 {
x ^= num;
} else {
y ^= num;
}
}
(x, y)
}
fn main() {
let arr = [-1, 2, 3, 2, 3, 4];
let result = find_two_unique(&arr);
println!("({}, {})", result.0, result.1);
}Attempts:
2 left
💡 Hint
Remember XOR works with negative numbers too. The order depends on grouping by the rightmost set bit.
✗ Incorrect
The XOR groups separate -1 and 4 correctly. The output is (-1, 4) because -1 falls in the group with the set bit.
🚀 Application
expertNumber of items in the resulting vector after grouping by rightmost set bit
Given the array [6, 1, 4, 1, 6, 9], after computing xor_all and grouping elements by the rightmost set bit of xor_all, how many elements are in the group where the bit is set?
DSA Rust
let arr = [6, 1, 4, 1, 6, 9]; let xor_all = arr.iter().fold(0, |acc, &x| acc ^ x); let rightmost_set_bit = xor_all & (-xor_all); let group_set_bit: Vec<i32> = arr.iter().cloned().filter(|&x| x & rightmost_set_bit != 0).collect(); println!("{}", group_set_bit.len());
Attempts:
2 left
💡 Hint
Calculate xor_all first, find its rightmost set bit, then count elements with that bit set.
✗ Incorrect
xor_all = 6 ^ 1 ^ 4 ^ 1 ^ 6 ^ 9 = 13 (binary 1101). The rightmost set bit is 1 (binary 0001). Elements with bit 1 set: 6 (110) no, 1 (001) yes, 4 (100) no, 1 (001) yes, 6 (110) no, 9 (1001) yes. So elements with bit 1 set are 1, 1, 9 → 3 elements.
