edited
2,893 views
1 votes
1 votes

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:

edited

3 Answers

Best answer
4 votes
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 votes
3 votes

time complexity will be O(n4)

–1 votes
–1 votes
T(n)=2T(n/2)+n^2 rr equation of recusive program

So O(n)^2

Related questions

0 votes
0 votes
0 answers
1
usdid asked Apr 16, 2022
278 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 votes
1 votes
1 answer
2
1 votes
1 votes
1 answer
3
0 votes
0 votes
1 answer
4
saptarshiDey asked Jan 3, 2019
8,174 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...