The GATE Grind

GATE 2013 CS (CS1) – Question 41

Algorithms · Asymptotic Analysis and Time/Space Complexity · 2 marks · Multiple choice

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;
}
  1. $\Theta(n^2)$
  2. $\Theta(n^2\log n)$
  3. $\Theta(n^3)$
  4. $\Theta(n^3\log n)$

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

All Asymptotic Analysis and Time/Space Complexity questions →