java - O-notation with for-loop with modulo condition -
my question how calculate o-notation operation, 2 outer loops go o(n^3) times. question o-notation going when modulo used in condtion , inner for-loop runs when factor in j.
for(int = 1 ; <= n ; i++) { for(int j =1; j <= ∗ ; j++) { if(j % == 0 ) { for(int k = 0 ; k < j ; k++ ){ sum++; } } } }
it o(n^4).
the trick 1 if (j % == 0). since i pretty n , j going range n^2 know statement true n + (n-1) + ... + 1 times going simplify (n+1)(n+2)/2 or o(n^2) in turn run loop runs n^2 times. need treat addition, have n^3 + n^2 * n^2, simplifies o(n^4).
if there fault in reasoning please point out, little rusty.
Comments
Post a Comment