Consider the following function: int unknown(int n) { int i, j, k=0; for (i=n/2; i The return value of the function is
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2013 · Q31
Time
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 isSelect one answerSingle correct
Solution
Official answer: B) (n2 log n)
Asymptotic AnalysisTo find the return value of the function, we determine how many times the inner statement executes and how much is added to in each step:
Outer loop:for (i = n/2; i <= n; i++)
The variable iterates from to , which is iterations.
Inner loop:for (j = 2; j <= n; j = j*2)
The variable doubles in each step starting from up to (), giving iterations.
Loop body:k = k + n/2;
In each execution of the innermost statement, the value is added to .
Total value of :
Thus, the return value of the function is , which corresponds to (B).
Try the question first, then check your work.
Examiner trap
Analyze the number of iterations of both the outer and inner loops, and multiply by the increment added to in each step.
This Asymptotic Analysis previous year question is Q31 of the GATE CSE 2013 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 2013 has 3.
How GATE tests Asymptotic AnalysisIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.