Exams › GATE › Technical
Consider the following function:
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);
}
The return value of the function is
- Θ(n²)
- Θ(n² log n)
- Θ(n³)
- Θ(n³ log n)
Correct answer: Θ(n² log n)
Solution
Outer loop runs about n/2 times, inner loop (j doubling to n) runs log2(n) times, and each iteration adds n/2. Total return value ~ (n/2)(log n)(n/2) = Theta(n^2 log n).
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →