Consider the following function:
int unknown (int n)
{
int i, j, k =0;
for (i = n/2; j < = n; i ++)
for (j = 2; j < = n; j = j*2)
k = k + n/2;
return (k);
}
The return value of the function is
A
Θ(n2)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
Θ(n3log n)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
Θ(n3)
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
Θ(n2log n)
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
Open in App
Solution
The correct option is DΘ(n2log n) Outer loop execute for n2 + 1 iterations. Inner loop execute for log2n iterations. In every iteration of inner loop n/2 is added to k.
Return value = n2× number of outer loops × number of inner loops