1 1 vote Is there Any short cut to do these questions fast ?Consider the following code \[ \begin{array}{l} \text { sum }=0 ; \\ \text { for }(\mathrm{i}=1 ; \mathrm{i} \leq n ; \mathrm{i}++) \\ \quad \text { for }\left(\mathrm{j}=1 ; \mathrm{j} \leq \mathrm{i}^{*} ; \mathrm{j}+++\right) \\ \quad \text { for }(\mathrm{k}=1 ; \mathrm{k} \leq \mathrm{j} ; \mathrm{k++}) \\ \quad \operatorname{iff}(\mathrm{j} \% \mathrm{i}=0) \text { sum++; } \end{array} \] What is the complexity of the above code? (a) $\theta\left(\mathrm{n}^{2}\right)$ (b) $\theta\left(\mathrm{n}^{3}\right)$ (c) $\theta\left(\mathrm{n}^{4}\right)$ (d) $\theta\left(\mathrm{n}^{5}\right)$ A B C D Answer: D, Algorithms time-complexity test-series + – Prince Sindhiya 1.5k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply Utkarsh Joshi commented Oct 23, 2018 i edited by Utkarsh Joshi Oct 24, 2018 reply Follow flag when i=1, j<=1, statement will be executed for 1 time when i=2, j<=4, the statement will be executed for 4 time when i=3, j<=9,statement will be executed 9 times So 1+4+9+16+..... sum of first n squares= n*(n+1)*(2n+1)/6 = O(n^3) 0 0 replyShare Prince Sindhiya commented Oct 23, 2018 i edited by Prince Sindhiya Oct 24, 2018 reply Follow flag @Utkarsh it is O(n^5) (edited) do it again 0 0 replyShare Prince Sindhiya commented Oct 23, 2018 reply Follow flag You forgot about 3 for loop 0 0 replyShare Hemanth_13 commented Oct 23, 2018 i edited by Hemanth_13 Oct 23, 2018 reply Follow flag Prince Sindhiya Could you please explain how ? I'm missing somewhere 0 0 replyShare Utkarsh Joshi commented Oct 23, 2018 i edited by Utkarsh Joshi Oct 24, 2018 reply Follow flag Idk what I forgot but according to me it would be n^3 only 0 0 replyShare Somoshree Datta 5 commented Oct 24, 2018 i edited by Somoshree Datta 5 Oct 24, 2018 reply Follow flag j executes for a total of 1+4+9+..+n2 times= n(n+1)(2n+1)/6. now for each value of j, k executes 1+2+3+..+j times. So when j executes n2 times, k executes for n2(n2+1)/2 times.. so time complexity is 1+(4*5/2)+(9*10/2)+..+(n2(n2+1)/2) = $\theta$(n5) Is this the right approach to follow? 0 0 replyShare Hemanth_13 commented Oct 24, 2018 reply Follow flag so time complexity is 1+(4*5/2)+(9*10/2)+..+(n2(n2+1)/2) = θ(n5) How was it n^5 ? 0 0 replyShare Somoshree Datta 5 commented Oct 24, 2018 reply Follow flag summation of 14+24+34+...+n4 is $\theta$(n5)..that is what i know 1 1 replyShare Hemanth_13 commented Oct 24, 2018 reply Follow flag Thank you :) Got my error. 0 0 replyShare Please log in or register to add a comment.
1 1 vote Answer would be O(n^5) Prince Sindhiya answered Oct 24, 2018 Prince Sindhiya comment Share Follow See all 2 Comments 2 2 Comments reply Utkarsh Joshi commented Oct 24, 2018 reply Follow flag I agree! it would be O(n^5) only! 0 0 replyShare Utkarsh Joshi commented Oct 24, 2018 reply Follow flag which test series is this? 0 0 replyShare Please log in or register to add a comment.