The time complexity of Fibonacci is O(2^n) What if I want to get time complexity of 3^n?
According to my friend the time complexity of fibonacci is O(2^n) due to following step:-
return fibonacci(n-1)+fibonacci(n-2)
Additionally he said that it will become O(3^n) if we write:-
return fibonacci(n-1)+fibonacci(n-2)+fibonacci(n-3)
Can somebody tell why is this so?
According to him it's because we are calling Fibonacci function twice at each step that's why it's O(2^n). If so, then the complexity of the following code should be O(k^k) but as per my knowledge it's O(n)
void permute(int k,int size) {
// some base case
for (i=k-1;i>=0;i--)
permute(k-1,size);
return;
}
His logics seems rubbish to me. To me it's 2^n due to the combine cost. Same goes for the Binary tree traversal cost it's O(n) despite of function calling itself twice at each step.
Can somebody tell me who is wrong amongst us and what's the actual logic?