• edited by
3,438 views
21 21 votes

Consider the recursive functions represented by the following code segment:

int bar(int n) {
    if (n == 1) return 0;
    else return 1 + bar(n/2);
}
int foo(int n) {
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
}

The smallest positive integer n for which $\mathrm{f} \circ \circ(\mathrm{n})$ returns $5$ is $\_\_\_\_$. (answer in integer)

Note: Ignore syntax errors (if any) in the function.

2 Answers

10 10 votes

First of all understand what both of the functions are really doing.

int bar(int n)
{
    if (n == 1) return 0;
    else return 1 + bar(n/2);
}

Each call divides $n$ by $2$ and adds $1$ until it reaches $1.$
So, basically, it is finding $\lfloor \log_2n \rfloor.$

$\therefore \bbox[4pt, border: 1px solid black]{bar(n) = \lfloor \log_2n \rfloor}$          $-$ $(1)$

int foo(int n)
{
    if (n == 0) return 0;
    else return 1 + foo(bar(n));
}

So, $foo(n) = 1 + foo(bar(n))$, using first equation we can write,

$\therefore \bbox[4pt, border: 1px solid black]{foo(n) = 1 + foo(\lfloor \log_2n \rfloor)}$          $-$ $(2)$

Base case : $foo(0) = 0$

Now, we need to find $n$ such that, $foo(n)=5$. So, we can open up $foo(n)$ in following way : 

Now, using base case we can say that, for $foo(log~log~log~log~log~n) = 0 $
$log~log~log~log~log~n = 0$

So, from this we can find $n$ as follows : 

​

$\mathbf{\therefore n = 2^{16} = 65536}$

• edited by
2 flags:
✌ Edit necessary (Prem_Kaushik “Base case of foo is wrong”)
✌ Edit necessary (luffy 56)
2 2 votes

bar(n) keeps dividing n by 2 until n becomes 1.

Examples:
bar(1) = 0
bar(2) = 1      (2 → 1)
bar(4) = 2      (4 → 2 → 1)
bar(16) = 4     (16 → 8 → 4 → 2 → 1)

foo(n) works as:
foo(n) = 1 + foo(bar(n))
and
foo(1) = 1

Now work backwards:

foo(1) = 1

To get foo(n) = 2:
bar(n) must be 1
Smallest n = 2

To get foo(n) = 3:
bar(n) must be 2
Smallest n = 4

To get foo(n) = 4:
bar(n) must be 4
Smallest n = 16

To get foo(n) = 5:
bar(n) must be 16
Smallest n whose bar(n) = 16 is 65536

(because 65536 = 2^16)

Verification:
foo(65536)
= 1 + foo(bar(65536))
= 1 + foo(16)
= 1 + (1 + foo(4))
= 1 + (1 + (1 + foo(2)))
= 1 + (1 + (1 + (1 + foo(1))))
= 1 + 1 + 1 + 1 + 1
= 5

Hence, the smallest positive integer n is:

65536

Answer:
Position:
Show:

Related questions

5 5 votes
3 3 answers
1.9k
1.9k views
gatecse asked Feb 23
1,948 views
The following sequence corresponds to the preorder traversal of a binary search tree $T$ :\[50,25,13,40,30,47,75,60,70,80,77\]The position of the element $60$ in the post...
8 8 votes
5 5 answers
3.7k
3.7k views
gatecse asked Feb 23
3,656 views
The height of a binary tree is the number of edges in the longest path from the root to a leaf in the tree. The maximum possible height of a full binary tree with $23$ no...
9 9 votes
4 4 answers
3.1k
3.1k views
gatecse asked Feb 23
3,143 views
Let $P$ be the set of all integers from $1$ to $15$. Consider any order of insertion of the elements of $P$ into a binary search tree that creates a complete binary tree....
14 14 votes
3 3 answers
2.1k
2.1k views
gatecse asked Feb 23
2,144 views
Let $G(V, E)$ be an undirected, edge-weighted graph with integer weights. The weight of a path is the sum of the weights of the edges in that path. The length of a path i...