edited by
28,963 views
80 80 votes

Consider the following recursive C function.

void get(int n)
{
    if (n<1) return;
    get (n-1);
    get (n-3);
    printf("%d", n);
}

If $get(6)$ function is being called in $main()$ then how many times will the $get()$ function be invoked before returning to the $main()$?

  1. $15$
  2. $25$
  3. $35$
  4. $45$

5 Answers

Best answer
110 110 votes

Answer : Option B

$\text{T(n) = T(n-1) + T(n-3) + 2}$, here $\text{T(n)}$ denotes the number of times a recursive call is made for input $n$. $2$ denotes the two direct recursive calls.

$\text{T(n $\leq0$) = 0}$
$\text{T(1) = 2}$
$\text{T(2) = 4}$
$\text{T(3) = 6}$
$\text{T(4) = 10}$
$\text{T(5) = 16}$
$\text{T(6) = 24}$

So, answer is $\text{24 + 1}$ call from main = $25$.

edited by
5 5 votes

@Arjun Sir I think we can also develop the recurrence like : 

 T(n) = T(n-1) + T(n-3) +1 ; otherwise
 T(n) = 1 ; for  n < 1 

   Which means if the node at which  we are standing is greater than equal to one then we will calculate the calls in (n-1) part + in        (n-3) part + 1 (since the node on which we are standing is also a call) and in case if n<1 then that will yields only 1.

T(1)=3 , T(2)= 3 + 1 + 1 = 5, T(3) = 5 + 1 + 1 = 7 , T(4) = 7 + 3 + 1= 11 , T(5) = 11 + 5 + 1 = 17,
T(6) = T(5) + T(3) + 1 =  17 + 7 + 1 = 25. 

Answer:
Position:
Show:

Related questions

44 44 votes
7 answers 7 answers
12.4k
12.4k views
go_editor asked Feb 16, 2015
12,430 views
Suppose $c = \langle c[0], \dots, c[k-1]\rangle$ is an array of length $k$, where all the entries are from the set $\{0, 1\}$. For any positive integers $a \text{ and } n...
90 90 votes
14 answers 14 answers
30.9k
30.9k views
go_editor asked Feb 15, 2015
30,925 views
Let $f(n) = n$ and $g(n) = n^{(1 + \sin \: n)}$, where $n$ is a positive integer. Which of the following statements is/are correct?$f(n) = O(g(n))$$f(n) = \Omega(g(n))$On...
93 93 votes
6 answers 6 answers
28.2k
28.2k views
go_editor asked Feb 14, 2015
28,170 views
Consider the equality $\displaystyle{\sum_{i=0}^n} i^3 = X$ and the following choices for $X$:$\Theta(n^4)$$\Theta(n^5)$$O(n^5)$$\Omega(n^3)$The equality above remains co...
7 7 votes
4 answers 4 answers
8.3k
8.3k views
go_editor asked Feb 16, 2015
8,315 views
Consider the following software items: Program-$X$, Control Flow Diagram of Program-$Y$ and Control Flow Diagram of Program-$Z$ as shown belowThe values of McCabe's Cyclo...