GATE | GATE CS 2013 | Question 31
Consider the following function:
Explanation: Here we have to tell the value of k returned not the time complexity.
for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = j * 2) k = k + n/2; return k;
The outer loop runs n/2 times
The inner loop runs logn times.(2^k = n => k = logn).
Now looking at the value of k in inner loop, n is added to k, logn times as the inner loop is running logn times.
Therefore total time complexity is inner multiplied with outer loop complexity which (n for outer and nlogn for inner) n*logn.
Therefore the value of k after running the inner loop one time is n^2logn.
This solution is contributed by Parul Sharma.
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.