recategorized by
4,435 views
1 votes
1 votes

Worst case time complexity of following code? Please explain in detail.

void function(int n)
{
    int count = 0;
    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("*");
            }
}
recategorized by

1 Answer

0 votes
0 votes
yes its O(n^4)

we can take O(n^5) as it is tightest upper bound only if O(n^4) is not an option

 

i loop will be running n times

j loop runs in n^2 times

k loop runs at n/2 times

total = n^4/2 = O(n^4)

correct me if i am wrong
edited by

Related questions

0 votes
0 votes
1 answer
1
5 votes
5 votes
2 answers
2
shaurya vardhan asked Nov 2, 2017
1,663 views
Consider the following functionVoid func(int n){Int k=n;Int i=0;for(;i<n;i++){while(k>1){k>>=1;}}What is the worst case time complexity of the function?
4 votes
4 votes
1 answer
3
0 votes
0 votes
1 answer
4