(Translated by https://www.hiragana.jp/)
Practice Questions for Recursion | Set 5 - GeeksforGeeks
Open In App

Practice Questions for Recursion | Set 5

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Question 1
Predict the output of the following program. What does the following fun() do in general?  

C++
C Java Python3 C# JavaScript

Output
12

It calculates a*b (a multiplied b).

Time Complexity: O(log2(b))
Auxiliary Space:  O(log2(b))

Question 2 
In question 1, if we replace + with * and replace return 0 with return 1, then what does the changed function do? Following is the changed function. 

C++
C Java Python3 C# JavaScript

Output
64

It calculates a^b (a raised to power b).
Time complexity: O(log2b)
Auxiliary Space: O(log2b)

Question 3 
Predict the output of the following program. What does the following fun() do in general?  

C++
C Java Python3 C# JavaScript

Output
91
fun(99) = fun(fun(110)) since 99 ? 100
           = fun(100)    since 110 > 100
           = fun(fun(111)) since 100 ? 100
           = fun(101)    since 111 > 100
           = 91        since 101 > 100

Time complexity: O(n)
Auxiliary Space: O(n), to maintain function call into call stack

The returned value of fun() is 91 for all integer arguments n 101. This function is known as McCarthy 91 function. 
Please write comments if you find any of the answers/codes incorrect, or you want to share more information/questions about the topics discussed above.
 


Article Tags :
Practice Tags :

Similar Reads