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
The function has two nested loops: the outer loop runs for n/2 to n, which is Θ(n), and the inner loop runs logarithmically with respect to n (specifically, it doubles j each iteration). Therefore, the total complexity is the product of these two, resulting in Θ(n² log n).
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →