• edited by
3,637 views
3 3 votes
sum=0;
for(i=0;i<n;i++)
    for(j=0;j<i*i;j++)
        for(k=0;k<j;k++)
            sum++;

2 Answers

5 5 votes

See first loop will run O(n) time

Middle loop condition j<i*i so it will run O(n2 ) time (since i is running n time)

Last loop condition k<j so i will run O( n2 ) time (j is running n2 time)

So total time complexity=O(n5)

3 3 votes
The run time can be mentioned mathematically as,

$T(n) = \sum_{i=0}^{n-1}\sum_{j=0}^{i^2-1}\sum_{k=0}^{j-1}c$

i.e

$T(n) =c \sum_{i=0}^{n-1}\sum_{j=0}^{i^2-1}j$

$T(n) =c \sum_{i=0}^{n-1}\frac{(i^2-1)(i^2)}{2}$

$T(n) =c \sum_{i=0}^{n-1}(i^4 - i^2)$

considering the higher order term

$T(n) =O(c \sum_{i=0}^{n-1}(i^4) )$

$T(n) =O(n^5)$
Position:
Show:

Related questions

0 0 votes
2 answers 2 answers
2.0k
2.0k views
radha gogia asked Jul 7, 2018
1,961 views
foo(int n) { for(int i=0 ; i<n ;i++) for(int j=i ; j<=i*i ;j++) if(j%i==0) { for(int k=0;k<j;k++) printf("hii"); } } How to proceed here for analyzing the time complexity...
2 2 votes
2 answers 2 answers
1.3k
1.3k views
radha gogia asked Jun 28, 2018
1,274 views
For the below code : foo() { for(i=1;i<=n;i++) { for(j=0;j<i;j++) { for(k=0;k<j;k++) c++; } } } Here k is executing j-1 times j is executing i times and i is executing n^...
3 3 votes
1 1 answer
2.2k
2.2k views
Sanjay Sharma asked Feb 20, 2018
2,176 views
An array A is of length n has log( n) distinct numbers.What is the time complexity of sorting A by best comparison based algorithm ?
1 1 vote
1 1 answer
2.5k
2.5k views
Akriti sood asked Dec 22, 2016
2,452 views
What is the worst case time complexity of the following recurrence relation?T(n)=T(n/2)+T(n/4)+T(n/8)+n Θ(nlogn) Θ(n2) Θ(n) -i am solving like thisT(n) <...