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
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