Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

GATE | GATE-CS-2015 (Set 3) | Question 49

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Consider the following recursive JAVA function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()? 

Java




static void get (int n){
  if (n < 1)
    return;
  get(n - 1);
  get(n - 3);
  System.out.print(n);
}


(A)

15

(B)

25

(C)

35

(D)

45



Answer: (B)

Explanation:

                              get(6) [25 Calls]
                              /      \\
               [17 Calls] get(5)       get(3) [7 Calls]
                        /     \\
                    get(4)    get(2)[5 Calls]
                   /    \\ 
     [7 Calls] get(3)  get(1)[3 Calls]
                /     \\
             get(2)   get(0)
            /    \\
[3 Calls]get(1)  get(-1) 
   /  \\
get(0) get(-2)

We can verify the same by running below program. 

CPP




# include <stdio.h>
int count = 0;
 
void get (int n)
{
    count++;
    if (n < 1) return;
    get(n-1);
    get(n-3);
}
int main()
{
    get(6);
    printf(\"%d \", count);
}


Output: 25


Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads