395 views

3 Answers

1 votes
1 votes
Code snippet will generate given gemoetric equation-:

$f\left ( n \right )=\frac{n}{2^{0}}+\frac{n}{2^{1}}+\frac{n}{2^{2}}+...1 $

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

$\text{where }2^{k}=n,k=\log_{2} n$

 

$=n (1 \times \frac{1-(\frac{1}{2} )^{k+1}}{1-\frac{1}{2}})$

$=2n -\frac{n}{2^{k+1}}\approx n \text{i.e}    O(n)$
0 votes
0 votes
As we are dividing by 2 everytime so it’s log n base 2
0 votes
0 votes
Here you can easily see that " i " is starting from " n " and going upto 1 and further adding to it the value " i " is divided by 2 every time.

So let  say that n=2^4  so how many times the loop will get executed i.e log(n) times.

Basically when the loop controlling variable gets divided or multiplied by a number the complexity goes to log(n) to the base of that number

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
NeelParekh asked Jul 27, 2023
277 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
2 votes
2 votes
1 answer
4
h4kr asked Dec 30, 2022
465 views
What is the correct time complexity in $\theta()$ ?