Number of arrays of size N whose elements are positive integers and sum is K | Return nCr ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function countArray ( $ N , $ K ) { return binomialCoeff ( $ K - 1 , $ N - 1 ) ; } $ N = 2 ; $ K = 3 ; echo countArray ( $ N , $ K ) ; ? >