GATE 2017 CS (CS2) – Question 48
Find the time complexity.
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);
}
}
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) $\Theta(n\log n)$
Explanation
For each i, the inner loop takes Θ(n/i) iterations, up to constant endpoint effects. Summing over i gives $\Theta(n\sum_{i=1}^n1/i)=\Theta(nH_n)=\Theta(n\log n)$. Thus C; the increasing step size prevents quadratic work.
More Asymptotic Analysis and Time/Space Complexity questions
- GATE 2012 CS (CS1) Q15
- GATE 2012 CS (CS1) Q28
- GATE 2013 CS (CS1) Q40
- GATE 2013 CS (CS1) Q41
- GATE 2011 CS (CS1) Q47
- GATE 2010 CS (CS1) Q22
All Asymptotic Analysis and Time/Space Complexity questions →