3 3 votes #include<stdio.h> int fun(int a,int b) { if(b==0) return 0; if(b%2==0) return fun(a+a,b/2); return fun(a+a,b/2)+a; } int main() { printf("%d",fun(9,11)); return 0; } Programming in C programming-in-c + – srestha 3.5k views answer comment Share Follow Print See all 18 Comments 18 18 Comments reply Arjun commented Nov 3, 2017 reply Follow flag Why did you use getchar? 3 3 replyShare srestha commented Nov 3, 2017 reply Follow flag oh, that was given in question I 'm removing that means it is a old version C question 0 0 replyShare joshi_nitish commented Nov 3, 2017 reply Follow flag it will return 72+18+9 = 99 // by traversing recursion tree topdown left to right.. 1 1 replyShare rahul sharma 5 commented Nov 3, 2017 reply Follow flag The second return will become unreachable statement? 0 0 replyShare joshi_nitish commented Nov 3, 2017 reply Follow flag why so? ...it will depend on truth value of if(b%2==0)....isn't it.. 0 0 replyShare rahul sharma 5 commented Nov 3, 2017 reply Follow flag yeah. i didnt notice if above that.My bad! 0 0 replyShare Shubhanshu commented Nov 3, 2017 reply Follow flag f(9,11) = 99. 0 0 replyShare Arjun commented Nov 3, 2017 reply Follow flag What is the time complexity of above code? 1 1 replyShare Anu007 commented Nov 3, 2017 reply Follow flag O(log2b) ? 1 1 replyShare Shubhanshu commented Nov 3, 2017 reply Follow flag yes, its TC should be log2b, because either b is even or odd its value is decreased be /2, which means its TC is log2b. And space complexity should also be log2b. 0 0 replyShare srestha commented Nov 3, 2017 reply Follow flag @Arjun Sir for b=11, function called 4 times. So, time complexity will be $O\left ( \left \lceil logn \right \rceil \right )$ 0 0 replyShare joshi_nitish commented Nov 3, 2017 reply Follow flag @srestha, time complexity is defined for variables, if you take b=4, time complexity is simply O(1) 1 1 replyShare srestha commented Nov 3, 2017 reply Follow flag Space complexity depends on return fun(a+a,b/2)+a; only on that a So, how many times if block not called, it depends on that So, it will be min 0 times and max log n times 0 0 replyShare joshi_nitish commented Nov 3, 2017 reply Follow flag it is not the case, base condition for this function depends on 'b'....so entire time and space complexity depends on 'b'....and b is reducing to b/2 in every call(irrespective of truth value of if)...it not at all depends on 'a'... 1 1 replyShare srestha commented Nov 3, 2017 reply Follow flag @joshi_nitish printf("%d",fun(9,11)); As main contains this line , u r telling O(1), rt? U mean in progeam if specific value given, it will be O(1) otherwise log n right? 0 0 replyShare joshi_nitish commented Nov 3, 2017 reply Follow flag yes, if time complexity of fun(9,11) is asked it is O(1)....for fun(a,b), it will be O(logb) 1 1 replyShare srestha commented Nov 3, 2017 reply Follow flag But space depend on a, not b I disagree in this point Because we add and store the bits, space required for that b not require any storing 0 0 replyShare Arjun commented Nov 3, 2017 reply Follow flag @srestha Space complexity usually (always for GATE purpose) refers to the auxiliary space and not the space required for storing the inputs. Here, (or usually for most recursive codes), it depends on the maximum recursion depth -- which in turn determines the no. of times activation record (we can assume activation record size as constant as long as there are no no local variables created) gets created simultaneously. You can see the answer update. 1 1 replyShare Please log in or register to add a comment.
Best answer 2 2 votes 99 is the correct answer The given code is actually doing just multiplication without using '*'. We can $a*b$ by adding $a$ to $a$, $b-1$ times. The given code is a smarter way to do this reducing the time complexity to $O(\log b)$ instead of $O(b)$ for the naive approach. The same code also works for doing power function, if we replace $+$ with $\times$. Ashwani Kumar 2 answered Nov 3, 2017 • edited Nov 3, 2017 by Arjun Ashwani Kumar 2 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Answer is 99 fun(9,11)->fun(18,5)+9->fun(36,2)+18->fun(72,1)->fun(144,0)+72 saipriyab answered Nov 5, 2017 saipriyab comment Share Follow 0 reply Please log in or register to add a comment.