680 views
0 0 votes
main()
{
    int i,count;
    for (i=1; i<=n; i++)
    {
        for(i=1; i<=(n*n); i++)
        {
            for(i=1; i<=(n*n*n); i++)
            {
              count++;  
            }
        }
    }
}

What will be the time complexity of the given program?

2 Answers

3 3 votes

its O(n3) as the program reaches first time in the third loop it runs for i = n3. and when it comes back to loop2 it finds out that i>n2 now when it comes back to loop 1 it gets i>n so it terminates out making time complexity to be O(n3).

0 0 votes
O(n^3)

because i is common for all three loops .

so

first loop first iteration i=1

second loop first iteration i=1;

third loop i=1   1<n^3

               i=2

            .........................................................

             i=n^3+1<=n^3   false(exit from 3rd loop) till now n^3 times loop is run.       

   n^3+2<n^2  false(exit from second loop)

 n^3+3<n  false(exit from first loop)

soO(n^3)
Position:
Show:

Related questions

3 3 votes
1 1 answer
216
216 views
GO Classes asked Aug 31
216 views
The algorithm $\text{ALGSORT}$ sorts an array of distinct integers using comparisons.The function $\text{MININDEX(V,i,j)}$ returns the position of the smallest element in...
1 1 vote
1 1 answer
151
151 views
GO Classes asked Aug 29
151 views
Suppose Huffman coding is implemented as follows.Initially, the $n$ symbols are stored in a min priority queue according to their frequencies.The algorithm repeatedly per...
1 1 vote
1 1 answer
128
128 views
GO Classes asked Aug 26
128 views
Consider,f1(N): x = 0 for i = 0 to N - 1: x++ return xand,f2(N, R): x = 0 for i = 0 to N - 1: for j = 1; j <= R; j = j + j: x = x + f1(j) return xWhat is the order of gro...
2 2 votes
1 1 answer
389
389 views
KrishnaVardhan asked Oct 7, 2024
389 views
Even though there are two for loops some times the Time complexity will be the m+n and some times it will be m*n assuming loops run till m and n respectively.How do we di...