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 $)$ Algorithms time-complexity algorithms asymptotic-notations test-series + – gauravkc 1.6k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Jason commented Apr 5, 2018 reply Follow flag A) ?? 0 0 replyShare gauravkc commented Apr 5, 2018 reply Follow flag How? 0 0 replyShare Please log in or register to add a comment.
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. $ruthi answered Apr 5, 2018 • selected Apr 5, 2018 by gauravkc $ruthi comment Share Follow See all 3 Comments 3 3 Comments reply gauravkc commented Apr 5, 2018 reply Follow flag Thanks :) 0 0 replyShare Sivarama Subramanian commented Apr 6, 2018 reply Follow flag I can't properly understand this step I ,J ,K and H will give $\log_{3}n* (n^2 + n log n)$complexity and then I thought it will become $\log_{3}n* (n^2 )$ + $\log_{3}n* (nlog n )$ which will be O($n^2 log_3 n$) and that equals O($n^2 log n)$. Please correct me where I'm wrong 0 0 replyShare himgta commented Apr 6, 2018 reply Follow flag for comparing ,remove the common terms thus logn base3 got cancelled out,remaing is n^2 and nlogn..... most significant term is n^2. 0 0 replyShare Please log in or register to add a comment.