in Algorithms retagged by
605 views
0 votes
0 votes
for(k=1;k<(n+1);k++)
{
    for(m=1;m<(n+1);m+=k){
        x=x+1;
    }
}

What is the T.C. of the following code?


Is it $n^{2}$ or $n\log n$??

in Algorithms retagged by
by
605 views

4 Comments

so the answer must me $O(nLog(n))$ right?
0
0
depends on question and options
0
0
In that case we can take any function asymptotically greater than $O(nLog(n))$
0
0

1 Answer

2 votes
2 votes
Best answer
for k =1 , inner for loop iterates n times.

for k=2, inner for loop iterates  n/2 times.

for k =3 , inner for loop iterates n/3 times.

......................

for k= n , inner for loop iterates n/n time.


so total = n+n/2+n/3+......n/n = n(1+1/2+1/3+....)=O(nlogn)
selected by

Related questions