Consider the following C function. int fun1(int n) { int i,j,k,p,q=0; for (i=1; i 1; j=j/2) ++p; for (k=1; k Which one of the following most closely approximates the return value of the function fun1 ?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2015 Set 1 · Q31
Time
int fun1(int n) {
int i,j,k,p,q=0;
for (i=1; i<n; ++i) {
p=0;
for (j=n; j>1; j=j/2)
++p;
for (k=1; k<p; k=k*2)
++q;
}
return q;
}Which one of the following most closely approximates the return value of the function fun1?Select one answerSingle correct
Solution
Official answer: D) nlog(log n)
Asymptotic AnalysisLet us analyze the loops in fun1(int n):
Outer loop:for (i = 1; i < n; ++i) runs times, i.e., iterations.
First inner loop:for (j = n; j > 1; j = j / 2) ++p;
Variable is halved in each step starting from down to .
Thus, increments times, so after this loop, .
Second inner loop:for (k = 1; k < p; k = k * 2) ++q;
Variable doubles in each step starting from until it reaches .
Thus, the loop runs times.
In each iteration of this loop, is incremented by 1.
Since the outer loop executes times, the total increments to across all iterations are:
Therefore, the return value of fun1 most closely approximates , which corresponds to option (D).
Try the question first, then check your work.
Examiner trap
Analyze the value of after the first inner loop, and determine how many times doubles to reach .
This Asymptotic Analysis previous year question is Q31 of the GATE CSE 2015 Set 1 paper. Find more GATE CSE PYQs on Asymptotic Analysis in Algorithms, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Asymptotic Analysis PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Asymptotic Analysis has 31 GATE CSE PYQs across 19 papers from 2011 to 2026. 12 of them are 2-mark MCQs like this one, and GATE CSE 2015 Set 1 has 2.
How GATE tests Asymptotic AnalysisIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.