Skip to content
GATEiit

GATE CSE 2017 Session 2 Q48: Time complexity of nested loop with step i

Syllabus topic: Asymptotic worst-case time and space complexity

Question

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 Θ notation is

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2017 final answer key)

Concept used: The inner loop runs about n/i times, so the total work is a harmonic sum n∑i=1n1/i.

Concept notes

Loading notes…

First step: Count the inner-loop iterations for a fixed i, then add over i from 1 to n.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Asymptotic worst-case time and space complexity

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2017 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key