GATE | GATE-CS-2005 | Question 81

Consider the following C-function:

filter_none

edit
close

play_arrow

link
brightness_4
code

double foo (int n)
{
    int i;
    double sum;
    if (n = = 0) return 1.0;
    else
    {
        sum = 0.0;
        for (i = 0; i < n; i++)
            sum += foo (i);
        return sum;
    }
}

chevron_right


The space complexity of the above function is:
(A) O(1)
(B) O(n)
(C) O(n!)
(D) O(nn)


Answer: (B)

Explanation: Note that the function foo() is recursive. Space complexity is O(n) as there can be at most O(n) active functions (function call frames) at a time.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.