2 votes 2 votes void fun(intn) { int s=0; for(i=1;i<=n;i++) { for(j=1;j<=i*i;j++) { if(j%i==0) { for(k=1;k<=j;K++) s++ } } } } Algorithms time-complexity algorithms + – junaid ahmad asked Jan 22, 2018 junaid ahmad 449 views answer comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments Ashwin Kulkarni commented Jan 22, 2018 reply Follow Share Okay I made a small mistake I got $O(n^4)$ for i = 1, j =1, k =1 for i = 2, j = 4 k = 2+4 = 2(1+2) for i = 3, j = 9 k = 3+6+9 = 3(1+2+3) .. .. for i = n, j = n^2 k = n+2n+3n...n^2 = n(1+2+3...n) hence observe last term it is n(1+2+3...n) = n(n(n+1)/2) = O(n^3) Now second last term will be (n-1)(1+2+3...(n-1)) = O(n^3) Hence all n^3 upto n will sum up and it will be $O(n^4)$ 0 votes 0 votes junaid ahmad commented Jan 22, 2018 reply Follow Share n*O(n3) = O(n4); n is coz of outer loop na ? 0 votes 0 votes Ashwin Kulkarni commented Jan 22, 2018 reply Follow Share Yes $n$ times $O(n^3)$ will be $O(n^4)$ 1 votes 1 votes Please log in or register to add a comment.