• moved ago by
6,073 views
5 5 votes
int fun(int n)
{
    int count=0;
    for(int i=n; i>0; i/=2)
    for(int j=0; j<i; j++)
      count += 1;
    return count ;
}

6 Answers

Best answer
5 5 votes

Outer loop runs $\log_2 n$ times.

For $i = n$, inner loop runs $n$ times.

For $i = n/2$, inner loop runs $n/2$ times.

$\vdots$

For $i = 1$, inner loop runs $1$ time.

So, overall, the inner loop runs for

$T(n) = 1+2+4+8+ \cdots + n$ times.

This is a Geometric Progression, which can be written as:

$T(n) = 1+2+4+8+\cdots+n = 2n-1 = \mathcal{O}(n)$

Hence, option C is correct.

• selected by
5 5 votes
The outer loop iteration variable i is halved in each iteration. The inner loop will execute i times for each iteration of the outer loop. So, the

count += 1;

statement, which is the most repeated one and hence the one contributing to time complexity will be done

n + n/2 + n/4 + .... 1 times, which is a GP and we can approximate it as an infinite GP, so the summation will be a/(1-r), where a is the first term and r is the common ratio.

n/(1-1/2) = 2n = O(n)
2 2 votes
 for(int i=n; i>0; i/=2) -------------L1
    for(int j=0; j<i; j++)------------L2

L1 stops when (n/2k=1)

                        or k=log2n 

L2 iterrates total = n + n/2 + n/22 + ... + n/2log2n    (for log2n time) (it is finite series)

                        =n 1. ( (1- 1/2log2n)/(1-1/2))

                        =n x ( (1-1/n) / (1/2) )

                         = 2n-2

So it is O(n) ...

2 2 votes

The correct answer indeed will be $ O(N) $. 

Here is the explanation, Let consider $ N = 2^K $

then the outer loop will run up to $ (Log N) $ times.

Then total work done by the program can be represented as,

T(n) = $ \sum_{i=0}^{log N} \frac{N}{2^i} $

T(n) = $ \sum_{i=0}^{log N} \frac{2^K}{2^i} $         (We have considered $ N = 2^{K} $ )

T(n) = $ \sum_{i=0}^{log N} 2^{K-i} $ 

T(n) <= $ \sum_{i=0}^{log N} 2^{K} $ 

T(n) <= $ 2^{K+1} $

T(n) <= $ 2^{log N+1} $
 
T(n) = $ O(2N) $.
 
Hence Time complexity of the program is O(N).
 
• moved ago by
0 0 votes

See here T(n)=n+n/2+n/4+n/6+.............................logn term

                      =n(1+1/2+1/4+1/6+.....................1/2logn-1)

Let n=ak

T(n)=ak(1+1/2+1/4+1/6+..................1/2k-1)       .................(1)

Now solve decreasing GP=1+1/2+1/4+1/6+.....................1/2k-1

Here a=1 , r=1/2

S=a(1-r)n/1-r

  =(1-(1/2)k)/(1-1/2)

  =2(1-1/2k)

Now From Equation (1)....

T(n)=2ak(1-1/2k)

Since n=ak  SO,

T(n)=2n(1-1/2logn)

       =2n(1-1/nlog2)

So T(n)=O(n)

• moved ago by
Position:
Show:

Related questions

3 3 votes
1 1 answer
228
228 views
GO Classes asked Aug 31
228 views
The algorithm $\text{ALGSORT}$ sorts an array of distinct integers using comparisons.The function $\text{MININDEX(V,i,j)}$ returns the position of the smallest element in...
1 1 vote
1 1 answer
159
159 views
GO Classes asked Aug 29
159 views
Suppose Huffman coding is implemented as follows.Initially, the $n$ symbols are stored in a min priority queue according to their frequencies.The algorithm repeatedly per...
1 1 vote
1 1 answer
136
136 views
GO Classes asked Aug 26
136 views
Consider,f1(N): x = 0 for i = 0 to N - 1: x++ return xand,f2(N, R): x = 0 for i = 0 to N - 1: for j = 1; j <= R; j = j + j: x = x + f1(j) return xWhat is the order of gro...
2 2 votes
1 1 answer
390
390 views
KrishnaVardhan asked Oct 7, 2024
390 views
Even though there are two for loops some times the Time complexity will be the m+n and some times it will be m*n assuming loops run till m and n respectively.How do we di...