T(2) = 1
T(1) = 0
Ans is (3/2)* n - 2
My solution is :
T(n) = 2 T(n/2) + 2
T(n) = 4 T(n/4) + 4
T(n) = 8 T(n/8) + 6
T(n)=(2^k)T(n/2^k) + 2k
where k = log(n) ..... in base 2
as n/(2^k) = 1 for T(1)
I don't how to solve this type of question to get in terms of n only. Can anyone help me out?
I am a bit confused because as pointed out in the comments, the intial conditions don't exactly make sense. However, just taking T(2) = 1, we can obtain our result:
Since, $T(n) = 2*T(n/2) + 2 => T(n) = 2*(2*T(n/4) + 2) + 2 => T(n) = 2^2*T(n/2^2) + 2^2 + 2$
Proceeding further we get $T(n) = 2^2 * (2*T(n/2^3)+2) + 2^2 + 2 => T(n) = 2^3*T(n/2^3) + 2^3 + 2^2 + 2$
As you can see, the relation we get now is $T(n) = 2^k T(n/2^k) + 2 + 2^2 +...+2^k$
Now because I want to use the initial condition, let $n = 2^{k+1}$ Therefore, $T(n) = \frac{2^{k+1}}{2}T(\frac{2^{k+1}}{2^k}) + 2 + 2^2 +...+ 2^k$
Therefore, $T(n) = \frac{n}{2}T(2) + 2 + 2^2 +...+ 2^k$
Since, $2 + 2^2 +...+ 2^k$ is a geometric progression, using the formula for a geometric progression sum, we get $2 + 2^2 +..+2^k = \frac{2(1-2^k)}{1-2} = 2(2^k-1) = 2(\frac{n}{2} - 1)$
Using this result and the fact that T(2) = 1, we get $T(n) = \frac{n}{2}(1) + 2(\frac{n}{2}-1) = \frac{n}{2} + n - 2 = \frac{3n}{2} - 2$.