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:
C++
#include<bits/stdc++.h>
using namespace std;
int sumofsquare( int n)
{
int C[n+1][n+1];
int i, j;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= min(i, n); j++)
{
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
int sum = 0;
for ( int i = 0; i <= n; i++)
sum += (C[n][i] * C[n][i]);
return sum;
}
int main()
{
int n = 4;
cout << sumofsquare(n) << endl;
return 0;
}
|
Java
import static java.lang.Math.*;
class GFG{
static int sumofsquare( int n)
{
int [][] C = new int [n+ 1 ][n+ 1 ] ;
int i, j;
for (i = 0 ; i <= n; i++)
{
for (j = 0 ; j <= min(i, n); j++)
{
if (j == 0 || j == i)
C[i][j] = 1 ;
else
C[i][j] = C[i- 1 ][j- 1 ]
+ C[i- 1 ][j];
}
}
int sum = 0 ;
for (i = 0 ; i <= n; i++)
sum += (C[n][i] * C[n][i]);
return sum;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(sumofsquare(n));
}
}
|
Python3
def sumofsquare(n) :
C = [[ 0 for i in range (n + 1 )]
for j in range (n + 1 )]
for i in range ( 0 , n + 1 ) :
for j in range ( 0 , min (i, n) + 1 ) :
if (j = = 0 or j = = i) :
C[i][j] = 1
else :
C[i][j] = (C[i - 1 ][j - 1 ] +
C[i - 1 ][j])
sum = 0
for i in range ( 0 , n + 1 ) :
sum = sum + (C[n][i] *
C[n][i])
return sum
n = 4
print (sumofsquare(n), end = "\n" )
|
C#
using System;
class GFG {
static int sumofsquare( int n)
{
int [,] C = new int [n+1,n+1] ;
int i, j;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.Min(i, n); j++)
{
if (j == 0 || j == i)
C[i,j] = 1;
else
C[i,j] = C[i-1,j-1]
+ C[i-1,j];
}
}
int sum = 0;
for (i = 0; i <= n; i++)
sum += (C[n,i] * C[n,i]);
return sum;
}
public static void Main()
{
int n = 4;
Console.WriteLine(sumofsquare(n));
}
}
|
PHP
<?php
function sumofsquare( $n )
{
$i ; $j ;
for ( $i = 0; $i <= $n ; $i ++)
{
for ( $j = 0; $j <= min( $i , $n ); $j ++)
{
if ( $j == 0 || $j == $i )
$C [ $i ][ $j ] = 1;
else
$C [ $i ][ $j ] = $C [ $i -1][ $j -1]
+ $C [ $i -1][ $j ];
}
}
$sum = 0;
for ( $i = 0; $i <= $n ; $i ++)
$sum += ( $C [ $n ][ $i ] * $C [ $n ][ $i ]);
return $sum ;
}
$n = 4;
echo sumofsquare( $n ), "\n" ;
?>
|
Javascript
<script>
function sumofsquare(n)
{
let C = new Array(n+1);
for (let i = 0; i < C.length; i++) {
C[i] = new Array(2);
}
let i, j;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.min(i, n); j++)
{
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i-1][j-1]
+ C[i-1][j];
}
}
let sum = 0;
for (i = 0; i <= n; i++)
sum += (C[n][i] * C[n][i]);
return sum;
}
let n = 4;
document.write(sumofsquare(n));
</script>
|
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:
C++
#include<bits/stdc++.h>
using namespace std;
int factorial( int start, int end)
{
int res = 1;
for ( int i = start; i <= end; i++)
res *= i;
return res;
}
int sumofsquare( int n)
{
return factorial(n+1, 2*n)/factorial(1, n);
}
int main()
{
int n = 4;
cout << sumofsquare(n) << endl;
return 0;
}
|
Java
class GFG{
static int factorial( int start, int end)
{
int res = 1 ;
for ( int i = start; i <= end; i++)
res *= i;
return res;
}
static int sumofsquare( int n)
{
return factorial(n+ 1 , 2 *n)/factorial( 1 , n);
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(sumofsquare(n));
}
}
|
Python
def factorial(start, end):
res = 1
for i in range (start, end + 1 ):
res * = i
return res
def sumofsquare(n):
return int (factorial(n + 1 , 2 * n)
/ factorial( 1 , n))
n = 4
print (sumofsquare(n))
|
C#
using System;
class GFG {
static int factorial( int start, int end)
{
int res = 1;
for ( int i = start; i <= end; i++)
res *= i;
return res;
}
static int sumofsquare( int n)
{
return factorial(n+1, 2*n)/factorial(1, n);
}
public static void Main()
{
int n = 4;
Console.WriteLine(sumofsquare(n));
}
}
|
PHP
<?php
function factorial( $start , $end )
{
$res = 1;
for ( $i = $start ;
$i <= $end ; $i ++)
$res *= $i ;
return $res ;
}
function sumofsquare( $n )
{
return factorial( $n + 1,
2 * $n ) /
factorial(1, $n );
}
$n = 4;
echo sumofsquare( $n ), "\n" ;
?>
|
Javascript
<script>
function factorial(start, end)
{
let res = 1;
for (let i = start; i <= end; i++)
res *= i;
return res;
}
function sumofsquare(n)
{
return parseInt
(factorial(n+1, 2*n)/factorial(1, n), 10);
}
let n = 4;
document.write(sumofsquare(n));
</script>
|
Output:
70
Time Complexity: O(n)
Auxiliary Space: O(1)