0 votes 0 votes Algorithms algorithms time-complexity asymptotic-notation + – Vaishnavi01 asked Sep 20, 2018 Vaishnavi01 690 views answer comment Share Follow See all 11 Comments See all 11 11 Comments reply sakharam commented Sep 21, 2018 reply Follow Share Is it thetha(n3)? 0 votes 0 votes phaneendrababu commented Sep 21, 2018 reply Follow Share The answer is O(n^5) 0 votes 0 votes himgta commented Sep 21, 2018 reply Follow Share @phaneendrababu explain! 0 votes 0 votes sakharam commented Sep 21, 2018 reply Follow Share @phaneendrababu Even if we remove the if(j%i) statement we obtain thetha(n4) 0 votes 0 votes sakharam commented Sep 21, 2018 i edited by sakharam Sep 21, 2018 reply Follow Share For a value i=m such that 0<=m<=n j ranges from 0 to m2; The third loop is run when j=m,2m,3m,....m2 so total m times, and for remain m2-m iterations of j, third loop is not executed hence constant time for them. Hence the first time the loop run for m iterations, then 2m, 3m,.....m2 Total: 1m+2m+3m+...+m2 + the remaining m2-m iterations of j which run for $\Theta$(1) = m[1+2+...+m]+m2=$\Theta$(m3) When the algorithm is run for all values of i We get 03+13+23+33+...n3 =$\Theta$(n4) 0 votes 0 votes phaneendrababu commented Sep 21, 2018 i edited by phaneendrababu Sep 21, 2018 reply Follow Share The first outer loop runs 'O(n)' times.The first inner loop runs 'O(n^2-n)' times for each iteration.The second inner loop depends on j value.So,it runs 'O(n^2-n)' times . Total time complexity::O(n)*O(n^2-n)*O(n^2-n)=O(n^5). 2 votes 2 votes sakharam commented Sep 21, 2018 reply Follow Share @phaneendrababu But the second loop doesnt always run for n^2 iterations. 0 votes 0 votes phaneendrababu commented Sep 21, 2018 reply Follow Share but at least it runs half of the times of the second loop,by doing amortized analysis also we are not getting O(n) for that. 0 votes 0 votes Shaik Masthan commented Sep 21, 2018 reply Follow Share @sakharam your explanation was correct, it is θ( n4 ) only @phaneendrababu θ( n4 ) = O(n5) ----- it is also correct, but we choose lower bound always ===> it is θ( n4 ) 0 votes 0 votes phaneendrababu commented Sep 21, 2018 i edited by phaneendrababu Sep 21, 2018 reply Follow Share The outer loop has 'n' iterations.For each outer loop the first inner loop has 'n^2' iterations,For some iterations of the first inner loop the second inner loop has 'n(n+1)/2' iterations. So,the time complexity is O(n)*O(n^2)*O(n^2)==>O(n^5). 2 votes 2 votes Vaishnavi01 commented Sep 21, 2018 reply Follow Share Look at this once , this is from Narasimha Karumanchi ,IIT Bombay 2 votes 2 votes Please log in or register to add a comment.
0 votes 0 votes Answer is O(n4) nephron answered Oct 31, 2018 nephron comment Share Follow See all 0 reply Please log in or register to add a comment.