IAE Academy

IN THE CS SYLLABUS

Asymptotic complexity and recurrences

Asymptotic complexity describes how an algorithm's time and space grow with input size , independent of any machine. It is expressed with the notations , , , and and found by counting loops or solving recurrences (substitution, recursion tree, Master theorem). The two core skills here are counting exactly how many times a statement executes, and solving a recurrence for a recursive algorithm's running time. Every example is traced in full.

14 previous-year questions · 14 solved · 6 practice questions

1. Why asymptotic analysis, and the five notations

Asymptotic complexity describes how the running time (or memory used) of a program grows as the input size becomes large, ignoring machine-dependent constants and lower-order terms. Conventions used throughout these notes:

  • is a positive integer.
  • The functions compared are non-negative for large .
  • Logarithms are base 2 unless stated otherwise.
  • int is 4 bytes and char is 1 byte; arrays are stored row-major.
  • The worst case is meant whenever a question does not say otherwise.
  • Space complexity means auxiliary (extra) space, not counting the input, unless stated otherwise.

Each notation is a set of functions bounded relative to a reference function . , and allow the bound to be tight. and describe strict bounds: grows strictly slower or strictly faster than .

BIG-O (UPPER BOUND)

grows no faster than , up to a constant factor.

BIG-OMEGA (LOWER BOUND)

grows at least as fast as , up to a constant factor.

BIG-THETA (TIGHT BOUND)

and grow at the same rate. Equivalently, and both hold.

LITTLE-O (STRICT UPPER BOUND)

The inequality must hold for EVERY , not just for some (contrast with ). When for all large , this is equivalent to .

LITTLE-OMEGA (STRICT LOWER BOUND)

When for all large , this is equivalent to .

Properties of the notations

  1. and .

  2. Transpose symmetry: , and .

  3. Symmetry: .

  4. Transitivity: all five notations are transitive. For example, and give .

  5. Reflexivity: , and . However, is never or .

  6. Constant factors: for any constant .

  7. Sum rule: for non-negative functions. Hence a polynomial of degree with positive leading coefficient is .

  8. Product rule: if and then .

  9. The base of a logarithm does not matter: for constants . Also, .

  10. The base of an exponential DOES matter: . Also, , but is NOT .

CHANGE OF BASE FOR LOGARITHMS

The two logarithms differ only by the constant factor , which is why the base is irrelevant inside , and .

EXAM TIP

A fast way to compare two given functions and is to compute :

  • If the limit is 0, then . Hence , but NOT .
  • If it is a finite positive constant, then .
  • If it is , then . Hence , but NOT .

If the limit does NOT exist, the test tells you nothing, so go back to the definitions. For example, is even though has no limit.

EXAM TIP

Comparing logarithms is a useful trick, but it has a trap. If , then . But does NOT imply . For example, , yet is not .

EXAM TIP

is the only one of these notations that fixes the growth rate from both sides. An bound is only an upper bound. An algorithm that takes time is also , and the statement 'its running time is ' is TRUE. So read the question carefully:

  • If it asks which statements are true, a loose bound counts as true.
  • If it asks for THE complexity and offers several options, choose the tight bound (the bound), even when the options are written with .

2. Ordering growth rates

A common task is to order a mixed list of functions (logarithmic, polynomial, exponential, factorial) from slowest-growing to fastest-growing. The chain below is the reference. To place a function that is not in it, simplify the function first, then use the limit test.

Standard growth-rate ordering (slowest to fastest)

  1. (constant)

  2. for a constant (polylogarithmic)

  3. (and, in general, for a constant )

  4. (linear)

  5. and higher fixed powers ( constant)

  6. for a constant , e.g. . Different bases are different rates: .

LOG VS POLYNOMIAL VS EXPONENTIAL VS FACTORIAL

These three limits give the dominance rules:

  • Any positive power of beats any power of .
  • Any exponential with base greater than 1 beats any fixed power of .
  • beats any exponential with a constant base.

STIRLING'S APPROXIMATION AND LOG(N!)

Consequences:

  • and .
  • The elementary bounds are enough on their own to give .
  • This result is behind the lower bound on the number of comparisons for comparison-based sorting: a decision tree with leaves has height at least .

EXPONENT-LOG SWAP IDENTITY

Take of both sides: each becomes . Examples: , , , .

WORKED EXAMPLE

Order the following functions from slowest to fastest growing: , , , , (logarithms base 2).

Solution
  1. Simplify , a linear function.

  2. . Its ratio to is , so it is sub-linear.

  3. grows faster than , because the ratio is .

  4. Compare with . The ratio is , so is faster.

  5. . Its exponent grows without bound, so it eventually exceeds every fixed power of , including .

  6. Final order: .

Answer:

EXAM TIP

With log base 2, is exactly , not an exponential function of . Always check the base of the power against the base of the logarithm. Unlike on its own, where the base only changes a constant factor, the base here changes the growth rate: but .

3. Analysing loops: independent, nested and dependent

To find the complexity of a piece of code, count how many times the innermost statement executes as a function of , then simplify to the dominant term. Work from the inside out:

1. Find the innermost loop's iteration count in terms of the surrounding loop variables.
2. Sum (or multiply) across the outer loops.

Procedure for analysing loops

  1. Identify the statement whose execution count you are asked for, often the innermost body.

  2. A step loop for (i = a; i <= b; i += k) with integer runs times when , and 0 times when .

  3. For independent nested loops (the inner bound does not depend on the outer variable), multiply the iteration counts.

  4. For dependent nested loops (for example for (j = 1; j <= i; j++)), write the total as a summation over the outer variable and evaluate it in closed form.

  5. A loop that multiplies its index by a constant (e.g. i *= 2) runs times. This needs a start value i >= 1: starting from i = 0, the index never grows. A loop that divides by 2 (i /= 2, from n down to 1) is also .

  6. A loop that squares its index (i = i * i, starting from i = 2) runs times. A loop for (i = 1; i * i <= n; i++) runs times.

  7. Drop lower-order terms and constant factors only at the very end, after the exact count is found.

ARITHMETIC SERIES

This is the count when the inner loop runs exactly times as goes from 1 to . If the inner loop runs times, the exact count changes to , and times gives . Both are still when .

SUMS OF POWERS

This applies to triple loops such as from 1 to , from 1 to , from 1 to , and to inner loops that run times.

GEOMETRIC SERIES

When , the sum is . When , it is .

HARMONIC SERIES AND LOG(N!)

The harmonic sum appears when an inner loop steps by the outer index (inner count about ). The sum appears when an inner loop runs times.

MULTIPLICATIVE (LOGARITHMIC) LOOP

This is the exact count for for (i = 1; i <= n; i *= c). For example, with and the loop runs times, not 10.

WORKED EXAMPLE

Determine the exact number of times x++; executes as a function of ( a power of 2) for the code below, then give its complexity.

int x = 0;
for (int i = 1; i <= n; i = i * 2) {
    for (int j = 1; j <= i; j++) {
        x++;
    }
}
Solution
  1. Outer loop: takes the values , so it runs times.

  2. For each value of , the inner loop runs exactly times.

  3. Total count (geometric series).

  4. So x++ executes exactly times (for example, 15 times when ). The complexity is , NOT , even though there are two nested loops and the outer one is logarithmic. This is a common trap.

Answer: Exactly executions; .

WORKED EXAMPLE

Give the complexity of the number of times x++; executes.

int x = 0;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j += i) {
        x++;
    }
}
Solution
  1. For a fixed , takes the values . So the inner loop runs times.

  2. The total is . Each term lies between and .

  3. So the total lies between and . The harmonic sum is , so the total is .

Answer:

EXAM TIP

Two nested loops do NOT automatically mean , or even . Always compute the actual sum. In the first example above, a geometric series inside a logarithmic outer loop collapses to linear. In the second, an inner step of turns a -looking loop into .

4. Recurrence relations: substitution and recursion tree

A recursive algorithm's running time is naturally expressed as a recurrence: an equation that gives in terms of on smaller inputs. Two general methods also handle recurrences that the Master theorem cannot:

  • Substitution (unrolling): expand the recurrence repeatedly, or guess the answer and verify it by induction.
  • Recursion tree: draw the calls as a tree and add up the work level by level.

Substitution method (unrolling), step by step

  1. Write the recurrence for , then for or by substituting into the same formula.

  2. Repeat the substitution times until the base case (or ) appears.

  3. Express the accumulated non-recursive work as a summation over the substitutions.

  4. Determine in terms of : for example , or for a recurrence with base case .

  5. Evaluate the summation in closed form and simplify to notation.

Recursion tree method, step by step

  1. Draw the tree. Each node is a subproblem, labelled with its input size and the non-recursive work done at that call.

  2. Determine the branching factor (children per node) and how the size shrinks per level, for example .

  3. Compute the total non-recursive work at each level of the tree.

  4. Determine the depth of the tree: the level at which the size reaches the base case.

  5. Add the per-level totals, including the leaves (number of leaves × base-case cost), to get .

WORKED EXAMPLE

Solve , , for a power of 2, using a recursion tree.

Solution
  1. Level 0 (root): 1 node of size ; non-recursive work .

  2. Level 1: 2 nodes of size ; total work .

  3. Level : nodes of size ; total work . Every internal level contributes exactly .

  4. The size reaches 1 when , i.e. . So levels to are internal, which gives levels of work each.

  5. The leaf level has leaves, each costing , for a total of .

  6. Total: . Check: .

Answer:

EXAM TIP

The shape of the per-level totals decides the answer:

  • Equal at every level: the answer is (work per level) × (number of levels). This is Master theorem Case 2.
  • Shrinking GEOMETRICALLY going down (each level at most a constant fraction of the one above): the root dominates, and the total is . This is Case 3.
  • Growing GEOMETRICALLY going down: the leaves dominate, and the total is . This is Case 1.

The word 'geometrically' matters. In , level contributes . The levels shrink going down, but not geometrically, so the root does not dominate. The sum is .

Standard recurrences to recognise

Recurrence

Solution

Typical source

Binary search (worst case)

Recursive divide-in-half with constant combine, e.g. maximum of an array

Halving with linear work per call

Merge sort

Linear recursion, e.g. recursive factorial

Quicksort worst case (always the most unbalanced split)

Tower of Hanoi: exactly moves when

,

Naive recursive Fibonacci; this is but NOT

Repeated square-rooting of the size

5. The Master theorem

The Master theorem gives a direct closed-form answer for recurrences of the form . It requires:

  • constants and ;
  • asymptotically positive.

The answer does not change if is read as or .

The theorem does NOT apply to:

  • subtractive recurrences such as ;
  • unequal-size subproblems such as ;
  • non-constant or , as in .

These need substitution, a recursion tree or a change of variables.

MASTER THEOREM: GENERAL FORM

is the number of subproblems, is the factor by which the size shrinks, and is the work done outside the recursive calls (dividing plus combining). Compare with , which is the number of leaves of the recursion tree.

Master theorem: the three cases

Case

Condition on f(n)

Result

1

for some constant ( smaller by a polynomial factor)

2

for some constant ( equal, times an optional log power). The basic theorem states only ; is the standard extended form.

3

for some constant , AND the regularity condition holds for some constant and all large . Regularity holds automatically for or with .

MASTER THEOREM FOR POLYNOMIAL F(N)

This is the special case with a constant. It is the quickest form to apply when has no log factor.

Applying the Master theorem, step by step

  1. Read off , and from the recurrence, and check that and are constants.

  2. Compute the critical exponent , which gives the comparison function .

  3. Compare with . Is it polynomially smaller (Case 1), the same order possibly times with (Case 2), or polynomially larger with regularity (Case 3)?

  4. If is smaller or larger only by a LOG factor, the basic three-case theorem does not apply. For example, is smaller, but not polynomially. The standard extension covers these: gives when , and when . Otherwise, use a recursion tree.

  5. State the final answer in notation using the matched case.

WORKED EXAMPLE

Solve , .

Solution
  1. , , .

  2. Critical exponent: , so compare with .

  3. : polynomially smaller than with . This is Case 1.

  4. By Case 1, .

Answer:

WORKED EXAMPLE

Solve , with for .

Solution
  1. The size shrinks by square-rooting, not by a constant factor, so the Master theorem does not apply directly. Change variables.

  2. Let , so and . Define .

  3. The recurrence becomes .

  4. For : , , , and . This is Case 2 with , so .

  5. Substitute back : .

Answer:

EXAM TIP

Do not apply the Master theorem to a SUBTRACTIVE recurrence. and are not of the form ; solve them by substitution or a recursion tree. Those two are exponential in . Not every subtractive recurrence is: is and is .

6. Space complexity, and best/worst/average case

Space complexity counts the memory an algorithm uses as a function of . In these notes it means auxiliary space, the memory beyond the input itself:

  • recursion stack frames;
  • auxiliary arrays;
  • extra pointers and counters.

Some texts also count the input. Say which one you mean when you work a problem.

Best, worst and average case describe how the running time varies across different inputs of the SAME size , not across different sizes.

Reading space complexity off code

  1. Iterative code with only a fixed number of scalar variables (no arrays sized by ) uses auxiliary space.

  2. An auxiliary array or table sized in proportion to (e.g. a DP table or an output array) contributes space; a 2-D table contributes .

  3. For recursive code, the space is the MAXIMUM DEPTH of the call stack at any moment times the space per frame, not the total number of calls made.

  4. Two recursive calls made one after the other reuse the same stack depth. The function f(n-1) + f(n-1) takes time but only stack space.

  5. These notes assume no tail-call optimisation: a recursive call uses a new stack frame even when it is the last action of the function, unless a question says otherwise.

WORKED EXAMPLE

What is the auxiliary space complexity of the recursive factorial function below, for input ?

int fact(int n) {
    if (n <= 1) return 1;
    return n * fact(n - 1);
}
Solution
  1. Each call to fact pushes one stack frame. The frame stays on the stack until its recursive call returns, because the multiplication happens afterwards.

  2. The deepest point is when fact(1) is reached. At that moment fact(n), fact(n-1), ..., fact(1) are all active: frames, each of constant size.

  3. Auxiliary space is proportional to the maximum stack depth , i.e. , even though the function uses no arrays.

Answer:

Best, worst, and average case

Case

Meaning

When it is used

Worst case

The maximum running time over ALL inputs of size

The default meaning of 'complexity' unless stated otherwise; it gives a guarantee

Best case

The minimum running time over all inputs of size

For example, insertion sort on an already-sorted array takes

Average case

The expected running time over a specified distribution of inputs, often 'all permutations equally likely'

Needs a stated or implied probability distribution; without one, 'average case' is not defined

EXAM TIP

Best/worst/average case and the notations are two separate axes:

  • Best/worst/average case is about INPUT VARIATION at a fixed size .
  • , and are about GROWTH RATE as varies.

It is valid to say 'the worst-case time is ' (a tight bound on the worst case) or 'the best-case time is '. Do not read as 'best case' or as 'worst case'.

PREVIOUS-YEAR QUESTIONS

14 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 1 · GATE CSE 2026 Set 2 · GATE CSE 2025 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2024 Set 1 · GATE CSE 2024 Set 2 · GATE CSE 2023 · GATE CSE 2022 · GATE CSE 2020

GATE 2026 CS1 Q.17

GATE 2026 · CS1Q.17 · 1 mark · MCQ

Consider the following recurrence relations:

For all ,

Assume that for all , and .

Which one of the following options is correct?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
See the question in the paper
GATE 2026 CS1 Q.17: answer and solution

GATE 2026 CS2 Q.24

GATE 2026 · CS2Q.24 · 1 mark · MCQ

Consider the following functions, where is a positive integer.

Which one of the following options lists the functions in increasing order of asymptotic growth rate?

Note: Assume the base of log to be 2.

  1. (A)

    , , ,

  2. (B)

    , , ,

  3. (C)

    , , ,

  4. (D)

    , , ,

Answer and solution
See the question in the paper
GATE 2026 CS2 Q.24: answer and solution

GATE 2026 CS2 Q.38

GATE 2026 · CS2Q.38 · 2 marks · MCQ

Consider an array of integers of size . The indices of run from to . An algorithm is to be designed to check whether satisfies the condition given below.

Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
See the question in the paper
GATE 2026 CS2 Q.38: answer and solution

GATE 2025 CS2 Q.38

GATE 2025 · CS2Q.38 · 2 marks · MCQ

A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data structures:

P: Unsorted doubly linked list with pointers to the head node and tail node of the list.

Q: Min-heap implemented using an array.

R: Binary Search Tree.

Which ONE of the following options gives the worst-case time complexities for meld operation on instances of size of these data structures?

  1. (A)

    P: , Q: , R:

  2. (B)

    P: , Q: , R:

  3. (C)

    P: , Q: , R:

  4. (D)

    P: , Q: , R:

Answer and solution
See the question in the paper
GATE 2025 CS2 Q.38: answer and solution

GATE 2024 CS1 Q.17

GATE 2024 · CS1Q.17 · 1 mark · MCQ

Given an integer array of size , we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is

  1. (A)

    both and

  2. (B)

    but not

  3. (C)

    but not

  4. (D)

    neither nor

Answer and solution
See the question in the paper
GATE 2024 CS1 Q.17: answer and solution

GATE 2023 CS Q.54

GATE 2023 · CSQ.54 · 2 marks · MSQ

Consider functions Function_1 and Function_2 expressed in pseudocode as follows:

Function_1:

while n > 1 do
    for i = 1 to n do
        x = x + 1;
    end for
    n = ⌊n/2⌋;
end while

Function_2:

for i = 1 to 100*n do
    x = x + 1;
end for

Let and denote the number of times the statement "" is executed in Function_1 and Function_2, respectively.

Which of the following statements is/are TRUE?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
See the question in the paper
GATE 2023 CS Q.54: answer and solution

PRACTICE

6 questions to test yourself

PRACTICE 12 marks · MCQ

Consider the recurrence , with . Which of the following best describes ?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (C)

  1. , , so .

  2. with .

  3. This is Case 2 (with ), so .

  4. Check with a recursion tree ( a power of 2). Level has nodes of size , each costing , so the level totals . Summing over to gives . The leaves add . The total is .

PRACTICE 22 marks · MSQ

Let and . Which of the following statements are TRUE?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (A), (C)

  1. .

  2. Any positive power of dominates any power of , so this ratio tends to .

  3. Hence , and therefore : A is TRUE.

  4. , so : C is TRUE.

  5. would need bounded above, which is false, so B is FALSE. needs both and , so D is FALSE.

PRACTICE 31 mark · NAT

The C fragment below is run with n = 20. How many times is the statement count++; executed?

int count = 0;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        count++;
    }
}
Answer and solution

Answer:

  1. For each fixed , the inner loop runs exactly times, with going from 1 to .

  2. Total .

  3. At : .

PRACTICE 41 mark · MCQ

What is the time complexity, in terms of , of the following recursive C function? Assume both recursive calls are executed exactly as written, with no compiler optimisation.

int f(int n) {
    if (n <= 1) return 1;
    return f(n - 1) + f(n - 1);
}
  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (C)

  1. Each call does work besides its recursive calls, so the time is .

  2. Let be the number of calls made by f(n). Then and for .

  3. Unrolling gives . For example, .

  4. Hence the time is . Note that the stack depth, and so the auxiliary space, is only .

PRACTICE 52 marks · NAT

for a power of 3, with . If for constants and , what is ?

Answer and solution

Answer:

  1. , , so .

  2. : Case 2 with .

  3. So . (Exactly, .)

  4. Hence and , so .

PRACTICE 61 mark · NAT

The C fragment below is run with n = 1024. What is the final value of x?

int x = 0;
for (int i = 1; i <= n; i = i * 2) {
    for (int j = 1; j <= n; j++) {
        x++;
    }
}
Answer and solution

Answer:

  1. The outer loop takes , which is to : iterations, not 10.

  2. The inner loop is independent of and runs times per outer iteration.

  3. Total .

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

Asymptotic worst case time and space complexity

Preparing in Kolkata? IAE Academy's course for GATE CSE.