Predict the output of following program
C
#include <stdio.h>int f(int n){ if(n <= 1) return 1; if(n%2 == 0) return f(n/2); return f(n/2) + f(n/2+1);}int main(){ printf(\"%d\", f(11)); return 0;} |
(A)
Stack Overflow
(B)
3
(C)
4
(D)
5
Answer: (D)
Explanation:
On successive recursion F(11) will be decomposed into F(5) + F(6) -> F(2) + F(3) + F(3) -> F(1) + 2 * [F(1) + F(2)] -> 1 + 2 * [1 + F(1)] -> 1 + 2 * (1 + 1) -> 5.
Hence, option D is the correct answer.
Quiz of this Question
Please comment below if you find anything wrong in the above post

… [Trackback]
[…] Find More Information here to that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]
… [Trackback]
[…] Read More here to that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]
… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]
… [Trackback]
[…] There you will find 73953 more Info to that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]
… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]
… [Trackback]
[…] Read More Info here on that Topic: geeksforgeeks.org/algorithms-recursion-question-8-2/ […]