UGC-NET Jun 2012 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0059

Consider the following Pseudo-code segment: k:=0 for $i_1$:=1 to n for $i_2$ := 1 to $i_1$ : : : for $i_m$:=1 to $i_{m-1}$ k:=k+1 The value of k after the execution of this code shall be
  1. A
    $C(n+m-1, m)$
  2. B
    $C(n-m+1, m)$
  3. C
    $C(n+m-1, n)$
  4. D
    $C(n-m+1, n)$

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionEach pass of the innermost statement corresponds to one sequence\[n\ge i_1\ge i_2\ge\cdots\ge i_m\ge1,\]i.e. a choice of \(m\) values from \(\{1,\dots,n\}\) with repetition allowed (order fixed).\[\boxed{\text{multisets of size }m\text{ from }n\text{ items}=\binom{n+m-1}{m}}\]So \(k=C(n+m-1,m)\). Check \(m=1\): \(C(n,1)=n\); \(m=2\): \(C(n+1,2)=\frac{n(n+1)}2=\sum_{i=1}^n i\).Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com