GateAI
Asymptotic Analysis

GATE CSE 2017 Set 2 · Q38

MCQ2 marksMedium

Time

Consider the following C function.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 Θ\Theta notation is

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) (n log n)

Asymptotic Analysis

Let us analyze the number of times the printf statement is executed:

  1. The outer loop runs for i=1,2,3,…,ni = 1, 2, 3, \dots, n.

  2. For a fixed value of ii, the inner loop starts at j=1j = 1 and increases jj by ii at each step until j<nj < n.

  3. The number of iterations of the inner loop for a given ii is:

⌈n−1i⌉=Θ(ni)\left\lceil \frac{n - 1}{i} \right\rceil = \Theta\left(\frac{n}{i}\right)
  1. Summing the inner loop executions over all values of ii from 11 to nn:

T(n)=∑i=1nΘ(ni)=n∑i=1n1iT(n) = \sum_{i=1}^{n} \Theta\left(\frac{n}{i}\right) = n \sum_{i=1}^{n} \frac{1}{i}
  1. The summation ∑i=1n1i\sum_{i=1}^{n} \frac{1}{i} is the harmonic series HnH_n, which is Θ(log⁡n)\Theta(\log n).

T(n)=n⋅Θ(log⁡n)=Θ(nlog⁡n)T(n) = n \cdot \Theta(\log n) = \Theta(n \log n)

Therefore, option C is correct.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Asymptotic Analysis in GATE CSE

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 Analysis

More from GATE CSE 2017 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2017 Set 2 · Q38 · MCQ

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.