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

2 Answers

5 votes
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 votes
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)$

Related questions

0 votes
0 votes
2 answers
1
radha gogia asked Jul 7, 2018
1,572 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...
3 votes
3 votes
1 answer
3
Sanjay Sharma asked Feb 20, 2018
1,106 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 ?