The time complexity of the following C function is (assume )
int recursive (int n) {
if(n == 1)
return (1);
else
return (recursive (n-1) + recursive (n-1));
}View worked solution
Answer: D
To find the time complexity of the given recursive function, let represent the number of operations performed for an input .
From the code, we can formulate the recurrence relation as follows:
- Base case: For , the function performs a constant number of operations:
- Recursive step: For , the function makes two recursive calls to
recursive(n-1)and performs a constant-time addition:
Let us solve this recurrence relation using substitution:
Since both terms are dominated by , the time complexity is:
Hence, the correct option is D.
Free practice · Your answers on this page are not saved.
