Recursive Power Function Complexity
What is the time complexity of the recursive power function above with respect to exp?
public int power(int base, int exp) {
if(exp == 0) return 1;
int half = power(base, exp / 2);
if(exp % 2 == 0) {
return half * half;
} else {
return base * half * half;
}
}
A
O(n log n)
B
O(n)
C
O(log n)
D
O(2^n)
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE