GateAI
Asymptotic Analysis

GATE CSE 2015 Set 1 · Q31

MCQ2 marksMedium

Time

Consider the following C function.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

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) nlog(log n)

Asymptotic Analysis

Let us analyze the loops in fun1(int n):

  1. Outer loop:for (i = 1; i < n; ++i) runs (n−1)(n-1) times, i.e., Θ(n)\Theta(n) iterations.

  2. First inner loop:for (j = n; j > 1; j = j / 2) ++p;

    • Variable jj is halved in each step starting from nn down to 11.

    • Thus, pp increments ⌊log⁡2n⌋\lfloor \log_2 n \rfloor times, so after this loop, p=Θ(log⁡n)p = \Theta(\log n).

  3. Second inner loop:for (k = 1; k < p; k = k * 2) ++q;

    • Variable kk doubles in each step starting from 11 until it reaches pp.

    • Thus, the loop runs ⌊log⁡2p⌋=⌊log⁡2(log⁡2n)⌋=Θ(log⁡(log⁡n))\lfloor \log_2 p \rfloor = \lfloor \log_2 (\log_2 n) \rfloor = \Theta(\log(\log n)) times.

    • In each iteration of this loop, qq is incremented by 1.

  4. Since the outer loop executes n−1n-1 times, the total increments to qq across all iterations are:

    q=(n−1)×Θ(log⁡(log⁡n))=Θ(nlog⁡(log⁡n))q = (n-1) \times \Theta(\log(\log n)) = \Theta(n \log(\log n))

Therefore, the return value of fun1 most closely approximates nlog⁡(log⁡n)n\log(\log n), which corresponds to option (D).

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 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

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 2015 Set 1 has 2.

How GATE tests Asymptotic Analysis

More from GATE CSE 2015 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2015 Set 1 · Q31 · MCQ

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

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