GATE 2013 CS (CS1) – Question 41
What is the asymptotic return value, rather than running time?
int unknown(int n) {
int i,j,k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k=k+n/2;
return k;
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (B) $\Theta(n^2\log n)$
Explanation
The outer loop executes Θ(n) times and the inner loop Θ(log n) times. Every iteration adds floor(n/2)=Θ(n) to k. Hence its return value is Θ(n·log n·n)=Θ(n² log n), B, assuming the mathematical asymptotic model without fixed-width overflow.
More Asymptotic Analysis and Time/Space Complexity questions
- GATE 2011 CS (CS1) Q47
- GATE 2010 CS (CS1) Q22
- GATE 2015 CS (CS2) Q16
- GATE 2015 CS (CS2) Q26
- GATE 2015 CS (CS3) Q20
- GATE 2015 CS (CS3) Q37
All Asymptotic Analysis and Time/Space Complexity questions →