Consider the following C function. int fun(int n) { int i, j; for(i = 1; i Time complexity of fun in terms of notation is
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2017 Set 2 · Q38
Time
int fun(int n) {
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j < n; j += i) {
printf(" %d %d",i,j);
}
}
}Time complexity of fun in terms of notation isSelect one answerSingle correct
Solution
Official answer: C) (n log n)
Asymptotic AnalysisLet us analyze the number of times the printf statement is executed:
The outer loop runs for .
For a fixed value of , the inner loop starts at and increases by at each step until .
The number of iterations of the inner loop for a given is:
Summing the inner loop executions over all values of from to :
The summation is the harmonic series , which is .
Therefore, option C is correct.
Try the question first, then check your work.
Examiner trap
Express the number of iterations of the inner loop as a function of and , then sum over all values of from to using the harmonic series.
This Asymptotic Analysis previous year question is Q38 of the GATE CSE 2017 Set 2 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 2017 Set 2 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.