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; } } |
chevron_right
filter_none
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
Recommended Posts:
- GATE | GATE-CS-2005 | Question 1
- GATE | GATE-CS-2005 | Question 2
- GATE | GATE-CS-2005 | Question 3
- GATE | GATE-CS-2005 | Question 4
- GATE | GATE-CS-2005 | Question 5
- GATE | GATE-CS-2005 | Question 6
- GATE | GATE-CS-2005 | Question 7
- GATE | GATE-CS-2005 | Question 8
- GATE | GATE-CS-2005 | Question 90
- GATE | GATE-CS-2005 | Question 10
- GATE | GATE-CS-2005 | Question 11
- GATE | GATE-CS-2005 | Question 12
- GATE | GATE-CS-2005 | Question 13
- GATE | GATE-CS-2005 | Question 14
- GATE | GATE-CS-2005 | Question 90
- GATE | GATE-CS-2005 | Question 16
- GATE | GATE-CS-2005 | Question 90
- GATE | GATE-CS-2005 | Question 90
- GATE | GATE-CS-2005 | Question 19
- GATE | GATE-CS-2005 | Question 90

