Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to XOR all elements in the array.
DSA C#
int xorResult = 0; foreach (int num in arr) { xorResult [1]= num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of XOR.
Using bitwise AND or OR instead of XOR.
✗ 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 xorResult.
DSA C#
int setBit = xorResult & -[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of xorResult for negative.
Using bitwise OR instead of AND.
✗ Incorrect
Using xorResult & -xorResult isolates the rightmost set bit.
3fill in blank
hardFix the error in the loop that separates elements into two groups based on setBit.
DSA C#
int x = 0, y = 0; foreach (int num in arr) { if ((num & [1]) != 0) x ^= num; else y ^= num; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using xorResult instead of setBit in condition.
Using 1 which checks only the least significant bit.
✗ Incorrect
Use setBit to check if the bit is set in num.
4fill in blank
hardFill both blanks to print the two non-repeating elements.
DSA C#
Console.WriteLine("The two non-repeating elements are: {0} and {1}", [1], [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Printing xorResult or setBit instead of x and y.
✗ Incorrect
x and y hold the two unique elements after separation.
5fill in blank
hardFill all three blanks to complete the method that returns the two unique elements as an array.
DSA C#
public static int[] FindTwoUnique(int[] arr)
{
int xorResult = 0;
foreach (int num in arr)
xorResult [1]= num;
int setBit = xorResult & -[2];
int x = 0, y = 0;
foreach (int num in arr)
{
if ((num & [3]) != 0)
x ^= num;
else
y ^= num;
}
return new int[] { x, y };
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of XOR.
Using wrong variable for negative.
Using xorResult instead of setBit in condition.
✗ Incorrect
Use XOR (^) to combine elements, xorResult for negative, and setBit for grouping.
