Given an array **arr[]** of n integers and an integer **K**, the task is to find the number of ways to select exactly **K** even numbers from the given array.

**Examples: **

Input:arr[] = {1, 2, 3, 4} k = 1Output:2Explanation:

The number of ways in which we can select one even number is 2.

Input:arr[] = {61, 65, 99, 26, 57, 68, 23, 2, 32, 30} k = 2Output:10Explanation:

The number of ways in which we can select 2 even number is 10.

**Approach:** The idea is to apply the rule of combinatorics. For choosing **r objects** from the given **n objects**, the total number of ways of choosing is given by ** ^{n}C_{r}**. Below are the steps:

- Count the total number of even elements from the given array(say
**cnt**). - Check if the value of
**K is greater than cnt**then the number of ways will be equal to 0. - Otherwise, the answer will be
^{n}C_{k}

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `long` `long` `f[12]; ` ` ` `// Function for calculating factorial ` `void` `fact() ` `{ ` ` ` `// Factorial of n defined as: ` ` ` `// n! = n * (n - 1) * ... * 1 ` ` ` `f[0] = f[1] = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= 10; i++) ` ` ` `f[i] = i * 1LL * f[i - 1]; ` `} ` ` ` `// Function to find the number of ways to ` `// select exactly K even numbers ` `// from the given array ` `void` `solve(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `fact(); ` ` ` ` ` `// Count even numbers ` ` ` `int` `even = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Check if the current ` ` ` `// number is even ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `even++; ` ` ` `} ` ` ` ` ` `// Check if the even numbers to be ` ` ` `// choosen is greater than n. Then, ` ` ` `// there is no way to pick it. ` ` ` `if` `(k > even) ` ` ` `cout << 0 << endl; ` ` ` ` ` `else` `{ ` ` ` `// The number of ways will be nCk ` ` ` `cout << f[even] / (f[k] * f[even - k]); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array arr[] ` ` ` `int` `arr[] = { 1, 2, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `arr / ` `sizeof` `arr[0]; ` ` ` ` ` `// Given count of even elements ` ` ` `int` `k = 1; ` ` ` ` ` `// Function Call ` ` ` `solve(arr, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `static` `int` `[]f = ` `new` `int` `[` `12` `]; ` ` ` `// Function for calculating factorial ` `static` `void` `fact() ` `{ ` ` ` ` ` `// Factorial of n defined as: ` ` ` `// n! = n * (n - 1) * ... * 1 ` ` ` `f[` `0` `] = f[` `1` `] = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= ` `10` `; i++) ` ` ` `f[i] = i * ` `1` `* f[i - ` `1` `]; ` `} ` ` ` `// Function to find the number of ways to ` `// select exactly K even numbers ` `// from the given array ` `static` `void` `solve(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `fact(); ` ` ` ` ` `// Count even numbers ` ` ` `int` `even = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Check if the current ` ` ` `// number is even ` ` ` `if` `(arr[i] % ` `2` `== ` `0` `) ` ` ` `even++; ` ` ` `} ` ` ` ` ` `// Check if the even numbers to be ` ` ` `// choosen is greater than n. Then, ` ` ` `// there is no way to pick it. ` ` ` `if` `(k > even) ` ` ` `System.out.print(` `0` `+ ` `"\n"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `// The number of ways will be nCk ` ` ` `System.out.print(f[even] / ` ` ` `(f[k] * f[even - k])); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given array arr[] ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Given count of even elements ` ` ` `int` `k = ` `1` `; ` ` ` ` ` `// Function call ` ` ` `solve(arr, n, k); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

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

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `static` `int` `[]f = ` `new` `int` `[12]; ` ` ` `// Function for calculating factorial ` `static` `void` `fact() ` `{ ` ` ` ` ` `// Factorial of n defined as: ` ` ` `// n! = n * (n - 1) * ... * 1 ` ` ` `f[0] = f[1] = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= 10; i++) ` ` ` `f[i] = i * 1 * f[i - 1]; ` `} ` ` ` `// Function to find the number of ways to ` `// select exactly K even numbers ` `// from the given array ` `static` `void` `solve(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `fact(); ` ` ` ` ` `// Count even numbers ` ` ` `int` `even = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Check if the current ` ` ` `// number is even ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `even++; ` ` ` `} ` ` ` ` ` `// Check if the even numbers to be ` ` ` `// choosen is greater than n. Then, ` ` ` `// there is no way to pick it. ` ` ` `if` `(k > even) ` ` ` `Console.Write(0 + ` `"\n"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `// The number of ways will be nCk ` ` ` `Console.Write(f[even] / ` ` ` `(f[k] * f[even - k])); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given array []arr ` ` ` `int` `[]arr = { 1, 2, 3, 4 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Given count of even elements ` ` ` `int` `k = 1; ` ` ` ` ` `// Function call ` ` ` `solve(arr, n, k); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(N)* **Auxiliary Space:** *O(N)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ways to express even number ‘n’ as sum of even integers
- Count ways to express a number as sum of exactly two numbers
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Average of even numbers till a given even number
- Count of ways to select K consecutive empty cells from a given Matrix
- Number of ways to select a node from each connected component
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to make exactly C components in a 2*N matrix
- Ways to select one or more pairs from two different sets
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Check if each element of the given array is the product of exactly K prime numbers
- Rearrange array such that all even-indexed elements in the Array is even
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to distribute exactly one coin to each worker
- Numbers less than N which are product of exactly two distinct prime numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.