• edited by
1,589 views
2 2 votes

What is the time complexity of this code?

What is the time complexity of the following code?
for (int $\mathrm{i}=\mathbf{1} ; \mathrm{i}<\mathbf{n} ; \mathrm{i}-\mathrm{i} * 3$ ) \{<br /> sum++;
for (int $j=0 ; j sum++;
for (int $k=n-1 ; k>0 ; k--$ )
sum + +;
for (int $\mathrm{h}=\mathrm{n}-1 ; \mathrm{h}>0 ; \mathrm{h}=\mathrm{h} / 2$ )
sum + +;
\} \}
$\mathrm{O}\left(\mathrm{n}^{2}\right)$
$\mathrm{O}\left(\mathrm{n}^{2} \log n\right)$
$\mathrm{O}\left(\mathrm{nlog}^{2} \mathrm{n}\right)$
$\mathrm{O}($ nlogn $)$

1 Answer

Best answer
1 1 vote
k and h will give n+logn complexity

J , k and h will give n(n+logn) complexity

I , j ,k and h will give

logn base 3(n^2+nlogn) complexity

Finally, n^2 logn base 3 + nlogn lognbase 3

Remove the negligibity and we get o(n^2) as complexity.
• selected by
Position:
Show:

Related questions

0 0 votes
1 1 answer
816
816 views
Beyonder asked Apr 11, 2018
816 views
2. What is asymptotical complexity of the following program: n= readInt()n = readInt()t = ms = 4for (int 1 = 0;105 += =m/= 2n=twriteInt(s) $O\left(n^{2} \times \log _...
3 3 votes
1 1 answer
1.4k
1.4k views
pranab ray asked Jan 15, 2018
1,447 views
Consider the following statements:\[\begin{array}{l}S_{1}: f(n)=\mathrm{O}\left((f(n))^{2}\right) \\S_{2}: \text { If } f(n)=\mathrm{O}(g(n)) \text { then } 2^{f(n)}=\mat...
0 0 votes
3 3 answers
800
800 views
pranab ray asked Jan 13, 2018
800 views
i am getting t.c as O(n^5) but given answer as O(n^4) what should be the answerWhat is the time complexity of the following code? void foo(int n) {int }s=0for (i=1;i\le...
0 0 votes
1 1 answer
964
964 views
Parshu gate asked Sep 17, 2017
964 views
What is the time complexity of the following code snippet? int j = 0;For (i=0;i