wiz-icon
MyQuestionIcon
MyQuestionIcon
3
You visited us 3 times! Enjoying our articles? Unlock Full Access!
Question

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

= n2 × (n2 + 1)(log n)
= O(n2 log n)

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Summation by Sigma Method
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon