Solving a Recursive equation, wrong options?

76 Views Asked by At

This is a very simple recursive formula. Are the options provided wrong?

$$ T(n) = T\left(\frac{n}2\right) + 2 $$ with $T(1) = 1$. What's the solution when $n=2^k$ ($n$ is a power of $2$)?

The answer I got is: $2\log(n) -1$

The options given were

$a)2(\log(n) + 1)$

$b)2\log(n)$

$c)\log(n) + 1$

$d)2\log(n) +1$

2

There are 2 best solutions below

1
On BEST ANSWER

Your answer fails fairly early on, since $$T(2)=T(1)+2=3$$ so $T(2)$ is not $2\log(2) - 1 = 2\cdot 1 -1 = 1$. My advice: always check your solutions.

0
On

$$n=2^k$$

So,the recursive relation becomes:

$$T(2^k)=T(2^{k-1})+2$$

We set $T(2^k)=S(k)$

$$S(k)=S(k-1)+2$$

$$S(k-1)=S(k-2)+2$$

$$\dots$$

$$S(2)=S(1)+2$$

So,we have $S(k)=S(1)+2 \cdot(k-1) $

$$S(1)=T(2)=3$$

$$S(k)=S(1)+2 \cdot(k-1)=3+2(k-1)=3+2k-2=2k+1 $$

$$S(k)=2k+1 \Rightarrow T(2^k)=2k+1 \Rightarrow T(n)=2 \lg n+1$$