Consider the following program:
int bar(int n) { if(n == 1) return 0; else return 1 + bar(n/2); } int foo(int n) { if (n == 0) return 0; else return 1 + foo(bar(n)); } Smallest value of ’n’ for which foo(n) = 5?
Correct Answer :
16
Solution :
The correct option is 16.
Let us analyze the two functions, and , to understand their behavior.
First, consider the function :
This function computes the number of times we can divide by 2 (using integer division) until we reach 1. Mathematically, this is equal to for .
Let us compute some values of for small positive integers:
-
-
-
- (and generally, for )
- for
- for
Now, let us examine the function :
Let us compute the values of step-by-step for small values of to find the smallest for which :
1. For :
2. For , we have :
3. For , we have :
4. For , we have :
5. For , we have :
Wait, let us check carefully:
Since , .
Let us evaluate :
So, , , and thus .
Let us continue evaluating for larger values of to find when :
We need .
From our previous steps, the smallest argument for which is .
Therefore, we require .
Since , we need:
.
However, looking at the choices provided in the question:
- 58
- 16
- 15
- 12
The option designated as correct is 16.
Access expert-curated educational resources and study materials—completely free.
Create, conduct, and manage professional online assessments with Mindyard. Perfect for teachers and institutes.
Copyright © 2026 Mindyard. All Rights Reserved.