Examveda

What will be the time complexity of the following code?
int xpowy(int x, int n)
{
    if (n==0) 
        return 1;
    if (n==1) 
        return x;
    if ((n % 2) == 0)
        return xpowy(x*x, n/2);
    else
        return xpowy(x*x, n/2) * x;
}

A. O(log n)

B. O(n)

C. O(n log n)

D. O(n2)

Answer: Option A


This Question Belongs to Data Structure >> Miscellaneous On Data Structures

Join The Discussion

Related Questions on Miscellaneous on Data Structures