Total number of Subsets of size at most K | Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate sum of nCj from j = 1 to k ; Calling the nCr function for each value of j ; Driver code
function binomialCoeff ( n , k ) { var C = new Array ( n + 1 ) ; for ( var i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; } var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function count ( n , k ) { var sum = 0 ; for ( var j = 1 ; j <= k ; j ++ ) { sum = sum + binomialCoeff ( n , j ) ; } return sum ; } var n = 3 ; var k = 2 ; document . write ( count ( n , k ) ) ; var n = 5 ; var k = 2 ; document . write ( count ( n , k ) ) ;