• retagged by
1,380 views
0 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$??

1 Answer

Best answer
2 2 votes
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
Position:
Show:

Related questions

6 6 votes
1 1 answer
3.8k
3.8k views
srestha asked May 18, 2019
3,799 views
Consider a procedure $find()$ which take array of $n$ integers as input, and produce pair of element of array whose difference is not greater than the difference of any o...
1 1 vote
1 1 answer
1.8k
1.8k views
srestha asked Apr 28, 2019
1,796 views
Given a sorted array of distinct integer $A\left [ 1,2,....n \right ]$, the tightest upper bound to check the existence of any index $i$, for which $A[i]=i$ is equal to _...
0 0 votes
1 1 answer
1.4k
1.4k views
aashish1406 asked Aug 9, 2023
1,440 views
Suppose we have a directed graph G = (V,E) with V= {1, 2, ..., n} and Eis presented as an adjacency list. For each vertex u in V, out(u) is a list such that (u, v) in {1,...
3 3 votes
1 1 answer
2.4k
2.4k views
aashish1406 asked Aug 9, 2023
2,429 views
Which of the following statement(s) is/are true?(a) Quicksort and merge sort are both examples of divide and conquer algorithms.(b) If we randomly choose a pivot element ...