Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to multiply the result by x when n is odd.
DSA C++
if (n % 2 == 1) result = result [1] x;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' causes wrong calculation.
Using '/' or '-' causes runtime errors or wrong results.
✗ Incorrect
When n is odd, multiply the current result by x to include the extra factor.
2fill in blank
mediumComplete the code to square x in each iteration.
DSA C++
x = x [1] x;Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds x to itself instead of squaring.
Using '-' or '/' leads to incorrect values.
✗ Incorrect
Squaring x means multiplying x by itself using '*'.
3fill in blank
hardFix the error in the loop condition to run while n is greater than zero.
DSA C++
while (n [1] 0) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run if n starts positive.
Using '==' runs only when n is zero, which is incorrect.
✗ Incorrect
The loop should continue while n is greater than zero to process all bits.
4fill in blank
hardFill both blanks to correctly halve n and update the loop.
DSA C++
n = n [1] 2; // Loop continues while n [2] 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '//' causes a syntax error because '//' starts a comment in C++.
Using '<' in the condition causes the loop to run incorrectly.
✗ Incorrect
Use integer division '/' to halve n and '>' to check if n is still positive.
5fill in blank
hardFill all three blanks to complete the fast exponentiation function.
DSA C++
int fastPower(int x, int n) {
int result = 1;
while (n [1] 0) {
if (n % 2 == 1) result = result [2] x;
x = x [3] x;
n = n / 2;
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' causes wrong multiplication.
Using '-' causes incorrect calculations.
✗ Incorrect
Loop while n > 0, multiply result by x when n is odd, and square x each iteration.
