Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to XOR all elements in the array.
DSA Rust
let mut xor_all = 0; for &num in &arr { xor_all [1]= num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) instead of XOR (^).
Using bitwise AND (&) or OR (|) which do not help find unique elements.
✗ Incorrect
XOR operator (^) is used to find the XOR of all elements.
2fill in blank
mediumComplete the code to find the rightmost set bit in xor_all.
DSA Rust
let set_bit = xor_all & [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using xor_all & xor_all which returns xor_all itself.
Using xor_all & !xor_all which results in 0.
✗ Incorrect
Rightmost set bit can be found by xor_all & (-xor_all).
3fill in blank
hardFix the error in the loop that separates elements into two groups based on set_bit.
DSA Rust
for &num in &arr { if num & [1] != 0 { x [2]= num; } else { y ^= num; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of & in the if condition.
Using + instead of ^= to accumulate XOR.
✗ Incorrect
Use bitwise AND (&) to check set bit and XOR (^=) to accumulate unique elements.
4fill in blank
hardFill both blanks to initialize variables and print the two unique numbers.
DSA Rust
let (mut x, mut y) = ([1], [2]); // After processing println!("Two non-repeating elements are: {} and {}", x, y);
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with xor_all which is incorrect.
Initializing with array elements which may cause wrong results.
✗ Incorrect
Initialize x and y to 0 before XOR accumulation.
5fill in blank
hardFill all three blanks to complete the function that returns the two unique numbers as a tuple.
DSA Rust
fn find_two_unique(arr: &[i32]) -> (i32, i32) {
let mut xor_all = 0;
for &num in arr {
xor_all [1]= num;
}
let set_bit = xor_all & [2];
let (mut x, mut y) = ([3], 0);
for &num in arr {
if num & set_bit != 0 {
x ^= num;
} else {
y ^= num;
}
}
(x, y)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^= for XOR.
Incorrect set_bit calculation.
Initializing x with xor_all instead of 0.
✗ Incorrect
Use ^= to XOR all elements, find set_bit with xor_all & (-xor_all), initialize x with 0.
