Bird
0
0
DSA Cprogramming~10 mins

Fast Exponentiation Power in Log N in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to multiply the result by x when n is odd.

DSA C
if (n % 2 != 0) {
    result = result [1] x;
}
Drag options to blanks, or click blank then click option'
A+
B-
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' causes incorrect power calculation.
Using '/' or '-' leads to wrong results or runtime errors.
2fill in blank
medium

Complete the code to square x in each iteration.

DSA C
x = x [1] x;
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds x to itself instead of squaring.
Using '/' or '-' causes wrong calculations.
3fill in blank
hard

Fix the error in the loop condition to run while n is greater than zero.

DSA C
while (n [1] 0) {
    // loop body
}
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run if n starts positive.
Using '==' runs loop only when n is zero, which is incorrect.
4fill in blank
hard

Fill both blanks to correctly halve n and update the loop.

DSA C
n = n [1] 1;
// loop continues while n [2] 0
Drag options to blanks, or click blank then click option'
A>>
B/
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' for halving is correct but slower than '>>'.
Using '<' in loop condition causes wrong loop behavior.
5fill in blank
hard

Fill 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 & 1) {
            result = result [2] x;
        }
        x = x [3] x;
        n = n >> 1;
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A>
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '*' breaks power calculation.
Wrong loop condition causes infinite or no loops.