Skip to content
Related Articles

Related Articles

GATE | GATE-CS-2005 | Question 82

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jun, 2021
View Discussion
Improve Article
Save Article

Consider the following C-function:




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;
    }
}


Suppose we modify the above function foo() and store the values of foo (i), 0 < = i < n, as and when they are computed. With this modification, the time complexity for function foo() is significantly reduced. The space complexity of the modified function would be:
(A) O(1)
(B) O(n)
(C) O(n!)
(D) O(nn)


Answer: (B)

Explanation: Space complexity now is also O(n).

We would need an array of size O(n). The space required for recursive calls would be O(1) as the values would be taken from stored array rather than making function calls again and again.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!