Count sequences of given length having non | C ++ program for the above approach ; Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long int binCoff ( unsigned int n , unsigned int r ) { unsigned long int val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } void findWays ( int M ) { int n = M / 2 ; unsigned long int a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; cout << b ; } int main ( ) { int M = 4 , X = 5 ; findWays ( M ) ; return 0 ; }