ISRO | ISRO CS 2020 | Question 21
What is the complexity of the following code ?
sum = 0;
for (i = 1; i <= n; i*= 2)
for(j = 1; j <= n; j++)
sum++;
Which of the following is not a valid string ?
(A) Ο(n2)
(B) Ο(n log n)
(C) Ο(n)
(D) Ο(n log n log n)
Answer: (C)
Explanation: According to Analysis of above Algorithm:
Inner loop will run unto n time, in maximum
Outer loop will run upto logn time, in maximum
So, total time complexity will be,
= O(n) * O(log n)
= O(n logn)
So, Ο(n log n log n) and Ο(n2) is also correct.
But it can not be less than O(n logn), so O(n) is not correct.
Quiz of this Question
Last Updated :
04 Sep, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...