GATE / CS ARCHIVE

GATE CSE 1992 / QUESTION 7(a)

Recurrence from nested loops

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 1992 paper

GATE 1992, Q7(a) Descriptive
Report issue

Asymptotic Analysis & Recurrences

Question 7(a), GATE 1992: The printed function initializes F1←1, tests n=1 and assigns F←3 in that case; otherwise it repeats i=1..n, setting C←0, incrementing C once for each j=1..F(n−1), then setting F1←F1*C. A final F←F1 appears after the loop, with ambiguous if/else grouping. n is positive. Derive a recurrence for the returned value F(n).
Question 7(a), GATE 1992: The printed function initializes F1←1, tests n=1 and assigns F←3 in that case; otherwise it repeats i=1..n, setting C←0, incrementing C once for each j=1..F(n−1), then setting F1←F1*C. A final F←F1 appears after the loop, with ambiguous if/else grouping. n is positive. Derive a recurrence for the returned value F(n).
Tap to enlarge
Answer & solution

Answer: Intended base-case-preserving reading: F(1)=3, F(n)=F(n−1)^n for n>1. Literal unconditional final assignment instead gives F(n)=1 for all n. (no official key was published)

First distinguish the function’s returned numerical value from its running time. In one outer iteration the inner loop increments C exactly F(n−1) times, so C=F(n−1). Starting from F1=1, the outer loop multiplies by that same value n times. Hence for n>1 the returned value is F(n)=∏[i=1..n] F(n−1)=[F(n−1)]n. The base is F(1)=3 if the base-case assignment is returned without being overwritten.

A block-explicit rendition of this intended reading is:

function F(n):
  if n=1 then return 3;
  F1:=1;
  for i:=1 to n do begin
    C:=0;
    for j:=1 to F(n−1) do C:=C+1;
    F1:=F1*C
  end;
  return F1.

This explains why neither n·F(n−1) (a sum) nor a runtime recurrence answers the value question. For instance F(2)=3²=9 and F(3)=9³=729.

The paper places a final F=F1 after the if/else loop without clearly enclosing it in the else branch. Under a literal Pascal-like reading with that assignment executed unconditionally, n=1 first sets F=3 but then overwrites it with the initial F1=1. The recurrence becomes F(1)=1, F(n)=F(n−1)n and all values are 1. The intended nontrivial recurrence therefore requires treating the final assignment as part of the else computation or returning immediately from the n=1 case.

Original paper, page 6