![]() |
VOOZH | about |
Given a positive integer n. The task is to find the sum of square of Binomial Coefficient i.e
nC02 + nC12 + nC22 + nC32 + ......... + nCn-22 + nCn-12 + nCn2
Examples:
Input : n = 4 Output : 70 Input : n = 5 Output : 252
Method 1: (Brute Force)
The idea is to generate all the terms of binomial coefficient and find the sum of square of each binomial coefficient.
Below is the implementation of this approach:
Output:
70
Time Complexity: O(n2)
Space Complexity: O(n2)
Method 2: (Using Formula)
=
=
Proof,
We know, (1 + x)n = nC0 + nC1 x + nC2 x2 + ......... + nCn-1 xn-1 + nCn-1 xn Also, (x + 1)n = nC0 xn + nC1 xn-1 + nC2 xn-2 + ......... + nCn-1 x + nCn Multiplying above two equations, (1 + x)2n = [nC0 + nC1 x + nC2 x2 + ......... + nCn-1 xn-1 + nCn-1 xn] X [nC0 xn + nC1 xn-1 + nC2 xn-2 + ......... + nCn-1 x + nCn] Equating coefficients of xn on both sides, we get 2nCn = nC02 + nC12 + nC22 + nC32 + ......... + nCn-22 + nCn-12 + nCn2 Hence, sum of the squares of coefficients = 2nCn = (2n)!/(n!)2.
Also, (2n)!/(n!)2 = (2n * (2n - 1) * (2n - 2) * ..... * (n+1))/(n * (n - 1) * (n - 2) *..... * 1).
Below is the implementation of this approach:
Output:
70
Time Complexity: O(n)
Auxiliary Space: O(1)