Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the variable that will hold the XOR result.
DSA C++
int unique = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing unique to 1 or -1 causes wrong results.
✗ Incorrect
We start with 0 because XOR with 0 leaves the number unchanged.
2fill in blank
mediumComplete the for loop to iterate over the array elements.
DSA C++
for (int i = 0; i [1] n; i++) { unique ^= arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= n causes out-of-bounds error.
✗ Incorrect
The loop should run from 0 to n-1, so use < n.
3fill in blank
hardFix the error in the XOR operation inside the loop.
DSA C++
unique [1] arr[i];Attempts:
3 left
💡 Hint
Common Mistakes
Using += or -= changes the logic and gives wrong answer.
✗ Incorrect
We use XOR assignment (^=) to combine elements.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA C++
int findUnique(int [1][], int [2]) { int unique = 0; for (int i = 0; i < n; i++) { unique ^= arr[i]; } return unique; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causes errors.
✗ Incorrect
The function takes an array named arr and its size n.
5fill in blank
hardFill all three blanks to complete the main function that calls findUnique and prints the result.
DSA C++
#include <iostream> using namespace std; int findUnique(int [1][], int [2]); int main() { int arr[] = {2, 3, 5, 4, 5, 3, 4}; int n = sizeof(arr) / sizeof(arr[0]); int result = findUnique([3], n); cout << "Unique element is: " << result << endl; return 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in function call.
✗ Incorrect
The function prototype and call use 'arr' and 'n' as parameters.
