Number of ways to select exactly K even numbers from given Array | Python3 program for the above approach ; Function for calculating factorial ; Factorial of n defined as : n ! = n * ( n - 1 ) * ... * 1 ; Function to find the number of ways to select exactly K even numbers from the given array ; Count even numbers ; Check if the current number is even ; Check if the even numbers to be chosen is greater than n . Then , there is no way to pick it . ; The number of ways will be nCk ; Given array arr [ ] ; Given count of even elements ; Function call
f = [ 0 ] * 12 NEW_LINE def fact ( ) : NEW_LINE INDENT f [ 0 ] = f [ 1 ] = 1 NEW_LINE for i in range ( 2 , 11 ) : NEW_LINE INDENT f [ i ] = i * 1 * f [ i - 1 ] NEW_LINE DEDENT DEDENT def solve ( arr , n , k ) : NEW_LINE INDENT fact ( ) NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( k > even ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( f [ even ] // ( f [ k ] * f [ even - k ] ) ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 1 NEW_LINE solve ( arr , n , k ) NEW_LINE