edited by
615 views
3 votes
3 votes
int j=0;
for(i=0;i<n;i++)
    {
    for(i=0;i<2n;i++)
        {
        while(j<n)
            {
            j++;
            }
        }
    }

time complexity.?

a.$O(n^{2})$                b.$O(n^{4})$                       c.$O(n^{3})$                    d.$O(n)$

edited by

1 Answer

Best answer
7 votes
7 votes
Trick about this question is that variable 'i' is shared and variable j is global.

Therefore, outer loop will only iterate once because on second iteration, i will be 2n due to inner for loop.

Also, while loop will also run for just once. Again, during second iteration, j will be n and hence condition will evaluate to false.

So this code runs for=>

2n(inner for loop) + n(while loop)

=> O(n)
selected by
Answer:

Related questions

1 votes
1 votes
2 answers
1
APOORV PANSE asked Jun 1, 2016
2,987 views
Consider the following c fragment : void DAA(n) { int i,j,k,x=0; for (i=1 ; i <=n ; i++) for (j=1 ; j <= i * i ; j++) { if ( j mod i == 0 ) for ( k = 1 ; k <= j ; k++) x=...
3 votes
3 votes
0 answers
2
Nikhil Patil asked Aug 15, 2017
458 views
What is Time Complexity of 4T ( n /2 ) + n / logn ?
1 votes
1 votes
1 answer
3
1 votes
1 votes
0 answers
4
Ankush Tiwari asked Jul 27, 2016
647 views
T(n)=sqrt(2T(n/2))+logn