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