in Algorithms
730 views
0 votes
0 votes

in Algorithms
730 views

11 Comments

Is it thetha(n3)?

0
0
The answer is O(n^5)
0
0

@phaneendrababu explain!

0
0

@phaneendrababu

Even if we remove the if(j%i) statement we obtain thetha(n4)

0
0
edited by

For a value i=m such that  0<=m<=n

j ranges from 0 to m2;

The third loop is run when j=m,2m,3m,....m2 so total m times, and for remain m2-m iterations of j, third loop is not executed hence constant time for them.

Hence the first time the loop run for m iterations, then 2m, 3m,.....m2

Total: 1m+2m+3m+...+m+ the remaining m2-m iterations of j which run for $\Theta$(1) = m[1+2+...+m]+m2=$\Theta$(m3)

When the algorithm is run for all values of i

We get  03+13+23+33+...n3

=$\Theta$(n4)

0
0
edited by
The first outer loop runs 'O(n)' times.The first inner loop runs 'O(n^2-n)' times for each iteration.The second inner loop depends on j value.So,it runs 'O(n^2-n)' times .

Total time complexity::O(n)*O(n^2-n)*O(n^2-n)=O(n^5).
2
2
@phaneendrababu

But the second loop doesnt always run for n^2 iterations.
0
0
but at least it runs half of the times of the second loop,by doing amortized analysis also we are not getting O(n) for that.
0
0

@sakharam

your explanation was correct, it is θ( n4 ) only

@phaneendrababu

θ( n4 )  = O(n5) ----- it is also correct, but we choose lower bound always ===> it is θ( n4 )

0
0
edited by
The outer loop has 'n' iterations.For each outer loop the first inner loop has 'n^2' iterations,For some iterations of the first inner loop the second inner loop has 'n(n+1)/2' iterations. So,the time complexity is O(n)*O(n^2)*O(n^2)==>O(n^5).
2
2

Look at this once , this is from Narasimha Karumanchi ,IIT Bombay This the solution given by Narasimha Karumanchi , IIT Bombay. But m still confused !!!

2
2

1 Answer

0 votes
0 votes

Answer is  O(n4)

Related questions