• edited by
19,525 views
14 14 votes

What is the complexity of the following code?

sum=0;
    for(i=1;i<=n;i*=2)
         for(j=1;j<=n;j++)
            sum++;

Which of the following is not a valid string?

  1. $O(n^2)$
  2. $O(n\log\ n)$
  3. $O(n)$
  4. $O(n\log\ n\log\ n)$

7 Answers

14 14 votes

$\underline{\textbf{Answer:}\Rightarrow}\;\mathbf{c.}$

Outer loop runs $\mathbf{\log n}$ times and inner loop runs $\mathbf{n}$ times.

$\therefore $ The time complexity will be: $\mathbf{O(n\log n)}$

They asked for the invalid string.

So, it will be $\mathbf{O(n)}$

$\underline{\underline{\color{red}{\textbf{Note:}}}}$ Question was $\color{green}{\textbf{excluded}}$ from the evaluation due to ambiguity.

• edited by
2 2 votes
The first loop runs for "log" time.

Second loop runs for "nahi" time.

Overall running complexity (as loop is inside the loop ,so their run time complexity gets multiplied )= O(nlogn).

So, the desired complexity is O(nlogn) , therefore the run time complexity can be greater than this but can never be lesser than this.

But according to question, Options A,B and D are correct and C is wrong . (we've to find out the wrong option ).

 

Hence correct answer is C
0 0 votes
Outer Loop :

i=1*2 :2

i=2*2 : 2^2

i=2^2*2 : 2^3

i=2^k and outer loop terminates if i>n substituting i=2^k

2^k>n

k=logn

and inner loop runs for n times giving overall timecomplexity as O(nlogn)
0 0 votes
Correct answer =>C

Time complexity Tight bound = Θ(nlogn)

option A)  nlogn <= c.n^2 (valid for large n)

option B )   nlogn <= c.nlogn (valid tight bound equal)

option C)   nlogn <=c.n (invalid as nlogn >n)

option D)   nlogn ,<= nlognlogn  (valid for large n)

 
0 0 votes
the correct option was c O(n)

we get the time complexity was nlogn and we have to compare with the remaining option ,if we take the frst option was n^2

nlogn<=n^2

nlogn <= n*n // if cancled the one n from the right side

logn <=n we know that n was greater when compare to the logn

in the same way we have to compare to the option c

nlogn <=n

nlogn<=c*n // c is the constant if we cancled the n from the right side we having the constant

we are know that "logn" is greater than the c but in the c option they was given the O(n) so that option c was invalid
Answer:
Position:
Show:

Related questions

5 5 votes
4 4 answers
5.9k
5.9k views
Satbir asked Jan 13, 2020
5,871 views
Consider product of three matrices $M_1,M_2$ and $M_3$ having $w$ rows and $x$ columns, $x$ rows and $y$ columns, and $y$ rows and $z$ columns. Under what condition will ...
9 9 votes
4 4 answers
9.2k
9.2k views
Satbir asked Jan 13, 2020
9,197 views
Huffman tree is constructed for the following data :$\{A,B,C,D,E\}$ with frequency $\{0.17,0.11,0.24,0.33\ \text{and} \ 0.15 \}$ respectively. $100\ 00\ 01101$ is decoded...
9 9 votes
5 5 answers
13.8k
13.8k views
Satbir asked Jan 13, 2020
13,767 views
If an array $A$ contains the items $10,4,7,23,67,12$ and $5$ in that order, what will be the resultant array $A$ after third pass of insertion sort?$67,12,10,5,4,7,23$$4,...
10 10 votes
3 3 answers
6.7k
6.7k views
Satbir asked Jan 13, 2020
6,733 views
In linear hashing, if blocking factor $bfr$, loading factor $i$ and file buckets $N$ are known, the number of records will be$cr= i+bfr+N$$r=i-bfr-N$$r=i+bfr-N$$r=i ^{\as...