edited by
1,040 views

4 Answers

Best answer
6 votes
6 votes
(The third loop executes only when j%i == 0, which will be true for j = i, 2i, 3i, ... i*i. i.e., it also executes i times. )

We have to count the number of times the statement $c = c + 1;$ is executed.

the outer $i$ loop executes from $1 .. n$.

for $i = 1, 2, 3, 4, \dots n$

the $k$ loop will execute for $j$ values

$(1), (2,4), (3,6,9), (4,8,12,16), \dots ,\left(n,2n,3n,\dots n^2\right)$

So, we have to count the iterations for k, which will give

$\sum_{i=1}^{n} \sum_{j=1}^{i} j \times i \\
= \sum_{i = 1}^{n}i.\sum_{j=1}^{i} j
\\= \sum_{i=1}^{n} i  \frac{i.(i+1)}{2}
\\= \sum_{i=1}^{n} \frac{i^3+i^2}{2}
\\= O(n^4)$

(We can assume the sum to the cubes of first n natural numbers)
edited by
1 votes
1 votes
I am not sure but see what we get.

i =  1  |   2  |   3  |   4  |  5   |  6  ........................n

j =  1t  | 4t  |  9t  | 16t | 25t |36t.................n^2t.                (t represent times)

k =  1   |   (2t 4t) |  (3t 6t 9t) | .................................

seeing just last value in every set we have

n(1 + 4 + 9 + 16 +...............n^2) + some constant

which is O(N4).
edited by
0 votes
0 votes
Time complexity of first loop will be n

Time complexity of second loop will be ((n^3/3)+(n^2/2)+(n/6))

Time complexity of third loop will be n*n^3

So, Total Complexity will be: O(n^4)
–1 votes
–1 votes
i varies from 1 to n

j varies from 1 to n*n

now j%i==0 follows 1 to n, 2n,3n,4n...n*n =2n-1 times

and k varies from 1 to n*n

therefore Time is proportional to n^6.

Is that right?

Related questions

1 votes
1 votes
1 answer
1
Markzuck asked Jan 6, 2019
501 views
Please show the ideal way to deal with such comparisons as I am getting g>=f IN genral what logic shall be followed to analyse such complex comparions?
0 votes
0 votes
1 answer
2
Markzuck asked Dec 29, 2018
791 views
cant we write the recurrance relation for bar() as T(n) = 5T(n-1) + c,like cant we take both the recurrance call as combined as both have same parameter?and if not, then ...
1 votes
1 votes
0 answers
3
1 votes
1 votes
1 answer
4