• retagged by
4,626 views
1 1 vote

The innermost loop will execute when j is multiple of i, and that will happen exactly i times. Please help me to find the time complexity of the below program:

3 Answers

Best answer
4 4 votes

For a given i, How many times if ( j % i == 0)  is true?

(For a given i), j can take the value 1, 2, 3, ....... (i * i).

So, whenever j take the value i, 2i, 3i, ..... (i*i). The condition if ( j % i == 0) is true.

So, for a given i, the condition if ( j % i == 0) is true for exactly i times. (Important).

Whenever the condition (if) is true how many times the innermost loop will execute?

For a given i, j can take the value 1, 2, 3, ....... (i * i).  (As mentioned above).

when j = i, the if condition is true, And the innermost loop will run i times.

when j = 2i, the if condition is true, And the innermost loop will run 2i times.

And so on,

When j = i * i, the if condition is true,  And the innermost loop will run i*i times.

Therefore, for a given i, the innermost loop will run for ( i + 2i + 3i + 4i + ....... + i*i) times.

And i will vary from i = 1 to n.

$\Rightarrow$  T(n) = $\sum_{i = 1}^{n} (i + 2i + 3i +..... (i*i))$

T(n) = $\sum_{i = 1}^{n} i*(1 + 2 + 3 + ...... i))$

T(n) = O($n^{4}$)

• selected
3 3 votes

time complexity will be O(n4)

Position:
Show:

Related questions

0 0 votes
0 0 answers
528
528 views
usdid asked Apr 16, 2022
528 views
a) what is the iterative equation showing the running time of the algorithm whose pseudocode is given below? b) What is this repeated equation in asymptotic notation usin...
1 1 vote
1 answers 1 answer
2.2k
2.2k views
Rishabh Gupta 2 asked Aug 31, 2017
2,230 views
Consider the following program segment: count = 0;for 1-1 to n{M= floor(n/1);for j-1 to mcount = count + 1;} The order of magnitude of the program segment is$\theta(n...
1 1 vote
1 answers 1 answer
1.2k
1.2k views
Khyati Tuli asked Jun 3, 2016
1,202 views
What should be the time complexity of : k=1; while (k<=n) do j=1; while (j<=k) do sum=sum+1; j=j+1; k=k*2;According to me:For K=1, j= 1 timefor K=2, j=2 timesFor K=4, j=4...
0 0 votes
1 answers 1 answer
9.0k
9.0k views
saptarshiDey asked Jan 3, 2019
9,013 views
What will be the worst case time complexity for the following code segment?int count=0,N; for(i=0;i<N*2;i++){ for(j=0;j<i/3;i++){ for(k=0;k<j*j;k++){ count++; } } }Option...