GATE | GATE 2017 MOCK II | Question 15

Consider the C code Below.

void function(int n)
{
    if (n == 1) 
      return;
    for (int i = 0; i<n; i++)
    {
        for (int j = 1; j< = n; j++)
        {
            printf("*");            
            break;
        }
    }
}

Which of the following is the tightest upper bound on time complexity of above function.

(A) O(n2)
(B) O(n)
(C) O(n log n)
(D) O(1)


Answer: (B)

Explanation: Important observation is Break statement terminates the innermost loop.
So “*” is printed only n times.


Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.