472 views

1 Answer

Best answer
5 votes
5 votes
for (i = 1; i <= n; i++) //Outer loop
    for(j = 1; j <= n; j = j + i)   // Inner loop
        x = x + 1;

Outer loop runs $n$ times

Now for Inner loop,

$i = 1, j = 1, 2, 3,….. n $  $\text{simply } \textbf {n} \text{ times}$

$i = 2, j = 1, 3, 5,….. n $  $\text{simply } \textbf {n/2} \text{ times}$

 

so for $n$ inner loop runs

$=> n + \frac{n}{2} + \frac{n}{3}+  \dots + \frac{n}{n}$

$=> n (1 + \frac{1}{2} + \frac{1}{3}+  \dots + \frac{1}{n})$

$=> n\  logn$

Therefore Overall time complexity is

$T(n) =  \mathcal O(n \ logn)$

 

selected by

Related questions

1 votes
1 votes
1 answer
2
0 votes
0 votes
1 answer
3
iarnav asked Jun 12, 2018
871 views
Given an sorted array in descending order, what will be the time complexity to delete the minimum element from this array?
1 votes
1 votes
2 answers
4
vishal chugh asked Jan 24, 2018
1,778 views
What is the worst case time complexity to find kth smallest element into an array of ‘n’ element?