retagged by
589 views

2 Answers

2 votes
2 votes
Following will be the equation for the given code snippet-:

    $n+\frac{n}{2}+\frac{n}{2^{2}}+\frac{n}{2^{3}}+...1$

$=n+\frac{n}{2}+\frac{n}{2^{2}}+\frac{n}{2^{3}}+...\frac{n}{2^{k}}$$\therefore 2^k=n$

$=n(\frac{1}{2^{0}}+\frac{1}{2^{1}}+\frac{1}{2^{2}}+\frac{1}{2^{3}}+...\frac{1}{2^{k}})$

=$n \times \frac{1-(\frac{1}{2})^{k}}{1-\frac{1}{2}}=2n -\frac{n}{2^k}=2n-1$
edited by
0 votes
0 votes
The number of times 'j' value will be incremented is dependent on 'i' value and here 'i' value is being halved every time so,

T(n)= $n$ + $\frac{n}{2}$ + $\frac{n}{4}$ + $\frac{n}{8}$ +.......+ $1$

= n [$1$ + $\frac{1}{2}$ + $\frac{1}{4}$ + $\frac{1}{8}$ +.......+$\frac{1}{n}$]

=n × O(1)

=O(n)
edited by

Related questions

0 votes
0 votes
2 answers
3