edited by
359 views
2 2 votes

Consider the following pseudocode. The program should take as input $n$, and when it ends, the element $A[i]$ of the vector $A$ should contain the binomial coefficient $\binom{n}{i}$ (with the index of $A$ starting from 0). In order to do so, what should replace the ___________ in the pseudocode?

procedure BINOMIALCOEFFICIENT
  input n
  for i ←  0 to n do
      A[i] ← 0
  end for
  A[0] ← 1
  for i ← 1 to n do
      B ← A
      for j ← 1 to n do
          A[j] ← ___________ 
     end for
   end for
end procedure
  1. $B(j-1)+B(j)$
  2. $A(j-1)+B(j-1)$
  3. $\dfrac{B(j)+B(n-j)}{2}$
  4. $\dfrac{B(j-1) \times B(j)}{2}$
  5. $\dfrac{A(j-1)+B(j-1)}{2}$

1 Answer

2 2 votes

In the beginning, $A$ is initialized as: $A = [1, 0, 0, ..., 0]$
 

The outer loop is running from $i = 1$ to $n$, where each iteration is basically calculating the coefficients for a new value of $n = i$
At the beginning of each outer loop iteration, the current array $A$ is copied to $B$

This means that $B[j]$ holds $\binom{i-1}{j}$ — the binomial coefficient values from the previous iteration and we are using these values for next.

We know that :
$$
\binom{i}{j} = \binom{i-1}{j-1} + \binom{i-1}{j}
$$
$B[j-1]$ is just $\binom{i-1}{j-1}$ 
and $B[j]$  is $\binom{i-1}{j}$ 

So, the inner loop is running from $j = 1$ to $n$, and updating $A[j]$ as:
$$
\boxed{A[j] = B[j - 1] + B[j]}
$$
 


Try for $n = 4$. 

Initially:  
$A = [1, 0, 0, 0, 0]$  
 

After $i = 1$:  
$A = [1, 1, 0, 0, 0]$  
$A$ stores $\binom{1}{j}$

After $i = 2$:  
$A = [1, 2, 1, 0, 0]$  
$A$ stores $\binom{2}{j}$

After $i = 3$:  
$A = [1, 3, 3, 1, 0]$  
$A$ stores $\binom{3}{j}$

After $i = 4$:  
$A = [1, 4, 6, 4, 1]$  
$A$ stores $\binom{4}{j}$

 

Answer:
Position:
Show:

Related questions

2 2 votes
1 1 answer
496
496 views
Shubham Sharma 2 asked Jun 16, 2025
496 views
What is the solution to the following recursion?$$\begin{array}{l}T(n)=T\left(\dfrac{n}{2}\right)+T\left(\dfrac{n}{3}\right)+T\left(\dfrac{n}{6}\right)+O(n), \\T(n)=5 \qu...
1 1 vote
1 1 answer
356
356 views
Shubham Sharma 2 asked Jun 16, 2025
356 views
Given a directed graph $G$ and an initial vertex $s$, we would like to $explore$ the graph from $s$, that is, starting from $s$ see all vertices along a path.For example,...
1 1 vote
0 0 answers
378
378 views
Shubham Sharma 2 asked Jun 16, 2025
378 views
Let $G=(V, E)$ be a weighted, undirected and connected graph, with weight $1 \leq$ $\mathrm{wt}_{G}(e) \leq 99$ for edge $e \in E$. Suppose $G^{\prime}$ is the graph with...
2 2 votes
1 1 answer
412
412 views
Shubham Sharma 2 asked Jun 16, 2025
412 views
Suppose $3$ elements are hashed independently and uniformly at random one by one to slots in a hash table of size $6$ (assume that in case of a collision, the element is ...