Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the XOR of all elements in the array.
DSA C++
int xor_all = 0; for (int i = 0; i < n; i++) { xor_all = xor_all [1] arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ causes incorrect results.
Using & or | does not isolate unique elements.
✗ Incorrect
XOR (^) is used to find the combined XOR of all elements.
2fill in blank
mediumComplete the code to find the rightmost set bit in xor_all.
DSA C++
int set_bit_no = xor_all & [1]xor_all;Attempts:
3 left
💡 Hint
Common Mistakes
Using ~ instead of - gives wrong bit isolation.
Using + or ! does not isolate the bit.
✗ Incorrect
Using -xor_all gives two's complement, isolating rightmost set bit with &.
3fill in blank
hardFix the error in the loop that separates elements into two groups based on set_bit_no.
DSA C++
int x = 0, y = 0; for (int i = 0; i < n; i++) { if (arr[i] & [1]) x = x ^ arr[i]; else y = y ^ arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using xor_all instead of set_bit_no causes wrong grouping.
Using i or n as mask causes errors.
✗ Incorrect
We check if the element has the set bit using set_bit_no mask.
4fill in blank
hardFill both blanks to print the two unique elements found.
DSA C++
std::cout << "The two non-repeating elements are: " << [1] << " and " << [2] << std::endl;
Attempts:
3 left
💡 Hint
Common Mistakes
Printing xor_all or set_bit_no instead of x and y.
Mixing up the variables.
✗ Incorrect
x and y hold the two unique elements after separation and XOR.
5fill in blank
hardFill all three blanks to complete the function that returns the two unique elements in a vector.
DSA C++
std::vector<int> findTwoUnique(int arr[], int n) {
int xor_all = 0;
for (int i = 0; i < n; i++) {
xor_all = xor_all [1] arr[i];
}
int set_bit_no = xor_all & [2]xor_all;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
return [3]{x, y};
}Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ for XOR.
Using ~ instead of - for two's complement.
Returning wrong container type.
✗ Incorrect
Use ^ for XOR, - for two's complement, and std::vector to return the result.
