Skip to content
GATEiit

GATE CSE · Algorithms

Asymptotic worst-case time and space complexity

Big-O, Omega and Theta notation, ordering functions by growth rate, counting loop iterations, solving recurrences with the master theorem and recursion trees, and measuring space including the recursion stack.

Before you start

  • Logarithms and exponents

    Comparing growth rates mostly means taking logs and using rules like alog⁡bn=nlog⁡ba.

  • Recurrence relations →

    Running times of recursive algorithms are written and solved as recurrences.

  • Recursion →

    Recursion depth decides the stack space an algorithm uses.

This topic comes before: Algorithm design techniques: greedy, dynamic programming and divide-and-conquer, Arrays, stacks, queues and linked lists, Searching, sorting and hashing

Concept notes

1. The notations and what they mean

For non-negative functions f and g:

  • f(n)=O(g(n)) (upper bound) if there are constants c>0 and n0 with f(n)≤c g(n) for all n≥n0.
  • f(n)=Ω(g(n)) (lower bound) if f(n)≥c g(n) for all n≥n0.
  • f(n)=Θ(g(n)) (tight bound) if both hold.
  • f(n)=o(g(n)) if f(n)/g(n)→0; f(n)=ω(g(n)) if f(n)/g(n)→∞.

Useful properties:

  • All five are transitive. O, Ω, Θ are reflexive; only Θ is symmetric.
  • f=O(g) if and only if g=Ω(f).
  • Limit test: if lim⁡f/g=c with 0<c<∞, then f=Θ(g).
  • Constant factors and lower-order terms do not matter: 3n2+10nlog⁡n+7=Θ(n2).
  • But constants in exponents do: 22n=4n is not O(2n), while 2n+1=2⋅2n is Θ(2n).

Worst-case complexity is the maximum cost over all inputs of size n. GATE asks for it unless the question says average or best case.

2. Ordering functions by growth

The standard order, slowest growth first (each is o of the next):

1≺log⁡log⁡n≺log⁡n≺(log⁡n)k≺n≺n≺nlog⁡n≺n2≺n3≺2n≺3n≺n!≺nn

Tricks for unfamiliar functions:

  • Take logs. Compare log⁡f and log⁡g; if log⁡f−log⁡g→∞ then f grows faster. For example nlog⁡n=2(log⁡n)2, which beats every polynomial 2klog⁡n but loses to 2n.
  • Any polylog loses to any polynomial: (log⁡n)100=o(n0.01), though only for very large n.
  • Any polynomial loses to any exponential: n100=o(1.01n).
  • alog⁡bn=nlog⁡ba, so 2log⁡2n=n and 4log⁡2n=n2.
  • Stirling: log⁡(n!)=Θ(nlog⁡n), and n!=o(nn).
  • Log base does not matter inside Θ: log⁡2n=Θ(log⁡10n). It does matter in an exponent.

When comparing two functions, the limit of the ratio settles it; if the limit does not exist (for example n1+sin⁡n), they may not be comparable at all.

3. Counting loops and measuring space

Time is the number of times the innermost statement runs.

Loop pattern Iterations
i = 1..n, step i++ n
i = 1; i <= n; i *= 2 ⌊log⁡2n⌋+1
i = n; i > 1; i = sqrt(i) Θ(log⁡log⁡n)
nested, j = 1..i inside i = 1..n n(n+1)/2
j steps by i up to n, inside i = 1..n ∑n/i=Θ(nlog⁡n) (harmonic)
j = 1..i inside i = 1, 2, 4, ..., n 1+2+⋯+n<2n, so Θ(n)

Useful sums: ∑i=1ni=n(n+1)2, ∑i=1ni2=Θ(n3), ∑i=1n1i=Θ(log⁡n), and a geometric series is dominated by its largest term.

Space complexity counts memory as a function of n. GATE usually means auxiliary space (beyond the input). Count:

  • arrays and other structures the algorithm allocates;
  • the recursion stack: depth of recursion times space per frame.

So recursive binary search uses Θ(log⁡n) stack space while the iterative one uses Θ(1). Merge sort needs Θ(n) for merging. Naive recursive Fibonacci takes exponential time but only Θ(n) space, because the stack never holds more than n frames.

4. Solving recurrences: master theorem and recursion tree

For T(n)=a T(n/b)+f(n) with a≥1, b>1, compare f(n) with nlog⁡ba:

Case Condition Result
1 f(n)=O(nlog⁡ba−ϵ) for some ϵ>0 Θ(nlog⁡ba)
2 f(n)=Θ(nlog⁡balog⁡kn), k≥0 Θ(nlog⁡balog⁡k+1n)
3 f(n)=Ω(nlog⁡ba+ϵ) and af(n/b)≤cf(n) for some c<1 Θ(f(n))

The master theorem does not apply to subtract-type recurrences. Use these instead:

  • T(n)=T(n−1)+1=Θ(n); T(n)=T(n−1)+n=Θ(n2).
  • T(n)=2T(n−1)+1=Θ(2n).
  • T(n)=T(n)+1=Θ(log⁡log⁡n) (substitute n=2m).

Recursion tree: draw the cost of each level and add the levels. For T(n)=2T(n/2)+n each level costs n and there are log⁡2n+1 levels, so T(n)=Θ(nlog⁡n). For unequal splits like T(n)=T(n/3)+T(2n/3)+n, every level costs at most n and the depth is log⁡3/2n, giving Θ(nlog⁡n).

nn/2n/2n/4n/4n/4n/4. . . 2^k nodes of size n/2^k . . .level cost nlevel cost nlevel cost nlog n levels
Recursion tree for T(n) = 2T(n/2) + n: every level costs n, and there are about log n levels

Formulas to remember

Big-O
f=O(g)  ⟺  ∃ c,n0:f(n)≤c g(n) ∀n≥n0
Theta
f=Θ(g)  ⟺  f=O(g) and f=Ω(g)
Master theorem critical exponent
T(n)=a T(n/b)+f(n),compare f(n) with nlog⁡ba
Master theorem, case 2
f=Θ(nlog⁡balog⁡kn)⇒T=Θ(nlog⁡balog⁡k+1n)
Arithmetic sum
∑i=1ni=n(n+1)2
Harmonic sum
∑i=1n1i=Θ(log⁡n)
Stirling (log form)
log⁡(n!)=Θ(nlog⁡n)
Log-power swap
alog⁡bn=nlog⁡ba

Common mistakes

  • Treating O as tight: n=O(n2) is true, but the tight bound is Θ(n).
  • Dropping constants in exponents: 4n is not O(2n), though 2n+1 is.
  • Applying the master theorem to T(n)=T(n−1)+f(n) or to cases where f is not polynomially larger or smaller (use the extended case 2 or a recursion tree).
  • Forgetting the regularity condition in case 3.
  • Ignoring the recursion stack when asked for space complexity.
  • Assuming two nested loops always give n2; a doubling inner loop gives nlog⁡n, and an inner loop bounded by a doubling outer index gives Θ(n).

Worked examples

Try each step yourself, then reveal the working.

Example 1: Arrange five functions by growth

numerical

Arrange in increasing order of asymptotic growth: f1=n1.5, f2=(log⁡n)10, f3=2n, f4=nlog⁡n, f5=2log⁡2n.

  1. Step 1: Simplify anything that hides a familiar function.

    Try it on paper first.

Example 2: Counting a doubling inner loop

numerical

How many times does count++ run for n=16, and what is the order in general?

for (i = 1; i <= n; i++)
  for (j = 1; j <= n; j = j * 2)
    count++;
  1. Step 1: Count the inner loop for $n = 16$.

    Try it on paper first.

Example 3: Three master theorem cases

numerical

Solve (a) T(n)=8T(n/2)+n2, (b) T(n)=4T(n/2)+n2, (c) T(n)=3T(n/4)+n.

  1. Step 1: (a) Find $n^{\log_b a}$ and compare with $f(n)$.

    Try it on paper first.

Example 4: Do constants in the exponent matter?

conceptual

Is 2n+1=O(2n)? Is 22n=O(2n)?

  1. Step 1: Rewrite $2^{n+1}$.

    Try it on paper first.

Previous year questions

21 official GATE CSE questions on this topic, newest first.

Practise these 21 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.