![]() |
VOOZH | about |
Given a positive integer n, the task is to find the sum of binomial coefficient i.e
nC0 + nC1 + nC2 + ....... + nCn-1 + nCn
Examples:
Input : n = 4 Output : 16 4C0 + 4C1 + 4C2 + 4C3 + 4C4 = 1 + 4 + 6 + 4 + 1 = 16 Input : n = 5 Output : 32
Method 1 (Brute Force):
The idea is to evaluate each binomial coefficient term i.e nCr, where 0 <= r <= n and calculate the sum of all the terms.
Below is the implementation of this approach:
Output:
16
Method 2 (Using Formula):
This can be proved in 2 ways.
First Proof: Using Principle of induction.
For basic step, n = 0
LHS = 0C0 = (0!)/(0! * 0!) = 1/1 = 1.
RHS= 20 = 1.
LHS = RHS
For induction step:
Let k be an integer such that k > 0 and for all r, 0 <= r <= k, where r belong to integers,
the formula stand true.
Therefore,
kC0 + kC1 + kC2 + ....... + kCk-1 + kCk = 2k
Now, we have to prove for n = k + 1,
k+1C0 + k+1C1 + k+1C2 + ....... + k+1Ck + k+1Ck+1 = 2k+1
LHS = k+1C0 + k+1C1 + k+1C2 + ....... + k+1Ck + k+1Ck+1
(Using nC0 = 0 and n+1Cr = nCr + nCr-1)
= 1 + kC0 + kC1 + kC1 + kC2 + ...... + kCk-1 + kCk + 1
= kC0 + kC0 + kC1 + kC1 + ...... + kCk-1 + kCk-1 + kCk + kCk
= 2 X ? nCr
= 2 X 2k
= 2k+1
= RHS
Second Proof: Using Binomial theorem expansion
Binomial expansion state,
(x + y)n = nC0 xn y0 + nC1 xn-1 y1 + nC2 xn-2 y2 + ......... + nCn-1 x1 yn-1 + nCn x0 yn
Put x = 1, y = 1
(1 + 1)n = nC0 1n 10 + nC1 xn-1 11 + nC2 1n-2 12 + ......... + nCn-1 11 1n-1 + nCn 10 1n
2n = nC0 + nC1 + nC2 + ....... + nCn-1 + nCn
Below is implementation of this approach:
Output:
16