Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the XOR of all elements in the array.
DSA Java
int xor = 0; for (int num : arr) { xor = xor [1] num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ^ causes incorrect results.
Using & or | operators does not find XOR.
✗ Incorrect
The XOR operator (^) is used to find the XOR of two numbers. Using xor = xor ^ num accumulates the XOR of all elements.
2fill in blank
mediumComplete the code to find the rightmost set bit in the XOR result.
DSA Java
int setBit = xor & -[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable instead of xor.
Using 1 or 0 instead of xor causes wrong bit isolation.
✗ Incorrect
Using xor & -xor isolates the rightmost set bit in xor.
3fill in blank
hardFix the error in the loop that separates elements into two groups based on the set bit.
DSA Java
int x = 0, y = 0; for (int num : arr) { if ((num & [1]) != 0) { x = x ^ num; } else { y = y ^ num; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using xor instead of setBit causes wrong grouping.
Using num or 0 in condition is incorrect.
✗ Incorrect
We use setBit to check if the bit is set in num to separate into two groups.
4fill in blank
hardFill both blanks to print the two non-repeating elements.
DSA Java
System.out.println("The two non-repeating elements are: " + [1] + " and " + [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Printing xor or setBit instead of x and y.
Mixing up the variable names.
✗ Incorrect
The variables x and y hold the two unique elements after separation and XOR.
5fill in blank
hardFill all three blanks to complete the method that returns the two unique elements in an array.
DSA Java
public static int[] findTwoUnique(int[] arr) {
int xor = 0;
for (int num : arr) {
xor = xor [1] num;
}
int setBit = xor & -[2];
int x = 0, y = 0;
for (int num : arr) {
if ((num & [3]) != 0) {
x = x ^ num;
} else {
y = y ^ num;
}
}
return new int[]{x, y};
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators or variables in blanks.
Mixing variable names causing errors.
✗ Incorrect
Use ^ for XOR operation, xor variable for negative, and setBit for grouping.
