GateAI
Asymptotic Analysis

GATE CSE 2013 · Q31

MCQ2 marksMedium

Time

Consider the following function: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 is

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) (n2 log n)

Asymptotic Analysis

To find the return value of the function, we determine how many times the inner statement executes and how much is added to kk in each step:

  1. Outer loop:for (i = n/2; i <= n; i++) The variable ii iterates from ⌊n/2⌋\lfloor n/2 \rfloor to nn, which is (n−n2+1)=Θ(n)\left(n - \frac{n}{2} + 1\right) = \Theta(n) iterations.

  2. Inner loop:for (j = 2; j <= n; j = j*2) The variable jj doubles in each step starting from 22 up to nn (j=21,22,…,2⌊log⁡2n⌋j = 2^1, 2^2, \dots, 2^{\lfloor \log_2 n \rfloor}), giving Θ(log⁡n)\Theta(\log n) iterations.

  3. Loop body:k = k + n/2; In each execution of the innermost statement, the value n2=Θ(n)\frac{n}{2} = \Theta(n) is added to kk.

  4. Total value of kk:

    Total k=(outer iterations)×(inner iterations)×n2\text{Total } k = (\text{outer iterations}) \times (\text{inner iterations}) \times \frac{n}{2}
    Total k=Θ(n)×Θ(log⁡n)×Θ(n)=Θ(n2log⁡n)\text{Total } k = \Theta(n) \times \Theta(\log n) \times \Theta(n) = \Theta(n^2 \log n)

Thus, the return value of the function is Θ(n2log⁡n)\Theta(n^2 \log n), which corresponds to (B).

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 2013 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 2013 has 3.

How GATE tests Asymptotic Analysis

More from GATE CSE 2013

Share this solution

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

GateAI solution cardGATE CSE 2013 · Q31 · MCQ

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

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