Given an array **arr[]** and an integer **K**, the task is to find number of non empty subsequence of length **K** from the given array **arr **of size** N **such that the product of subsequence is a even number.**Example:**

Input:arr[] = [2, 3, 1, 7], K = 3Output:3Explanation:

There are 3 subsequences of length 3 whose product is even number {2, 3, 1}, {2, 3, 7}, {2, 1, 7}.

Input:arr[] = [2, 4], K = 1Output:2Explanation:

There are 2 subsequence of length 1 whose product is even number {2} {4}.

**Approach:**

To solve the problem mentioned above we have to find the total number of subsequence of length K and subtract the count of K length subsequence whose product is odd.

- For making a product of the subsequence odd we must choose K numbers as odd.
- So the number of subsequences of length K whose product is odd is possibly
**finding k odd numbers**, i.e., “*o choose k*” or

where*o*is the count of odd numbers in the subsequence.

where*n*and*o*is the count of total numbers and odd numbers respectively.

Below is the implementation of above program:

## C++

`// C++ implementation to Count of K ` `// length subsequence whose ` `// Product is even ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `fact(` `int` `n); ` ` ` `// Function to calculate nCr ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `if` `(r > n) ` ` ` `return` `0; ` ` ` `return` `fact(n) ` ` ` `/ (fact(r) ` ` ` `* fact(n - r)); ` `} ` ` ` `// Returns factorial of n ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// Function for finding number ` `// of K length subsequences ` `// whose product is even number ` `int` `countSubsequences( ` ` ` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `countOdd = 0; ` ` ` ` ` `// counting odd numbers in the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] & 1) ` ` ` `countOdd++; ` ` ` `} ` ` ` `int` `ans = nCr(n, k) ` ` ` `- nCr(countOdd, k); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 4 }; ` ` ` `int` `K = 1; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << countSubsequences(arr, N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count of K ` `// length subsequence whose product ` `// is even ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate nCr ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `if` `(r > n) ` ` ` `return` `0` `; ` ` ` `return` `fact(n) / (fact(r) * ` ` ` `fact(n - r)); ` `} ` ` ` `// Returns factorial of n ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `res = res * i; ` ` ` ` ` `return` `res; ` `} ` ` ` `// Function for finding number ` `// of K length subsequences ` `// whose product is even number ` `static` `int` `countSubsequences(` `int` `arr[], ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `countOdd = ` `0` `; ` ` ` ` ` `// Counting odd numbers in the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % ` `2` `== ` `1` `) ` ` ` `countOdd++; ` ` ` `} ` ` ` `int` `ans = nCr(n, k) - nCr(countOdd, k); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `4` `}; ` ` ` `int` `K = ` `1` `; ` ` ` ` ` `int` `N = arr.length; ` ` ` ` ` `System.out.println(countSubsequences(arr, N, K)); ` `} ` `} ` ` ` `// This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Count of K ` `# length subsequence whose ` `# Product is even ` ` ` `# Function to calculate nCr ` `def` `nCr(n, r): ` ` ` ` ` `if` `(r > n): ` ` ` `return` `0` ` ` `return` `fact(n) ` `/` `/` `(fact(r) ` `*` ` ` `fact(n ` `-` `r)) ` ` ` `# Returns factorial of n ` `def` `fact(n): ` ` ` ` ` `res ` `=` `1` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `res ` `=` `res ` `*` `i ` ` ` ` ` `return` `res ` ` ` `# Function for finding number ` `# of K length subsequences ` `# whose product is even number ` `def` `countSubsequences(arr, n, k): ` ` ` ` ` `countOdd ` `=` `0` ` ` ` ` `# Counting odd numbers in the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] & ` `1` `): ` ` ` `countOdd ` `+` `=` `1` `; ` ` ` ` ` `ans ` `=` `nCr(n, k) ` `-` `nCr(countOdd, k); ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `arr ` `=` `[ ` `2` `, ` `4` `] ` `K ` `=` `1` ` ` `N ` `=` `len` `(arr) ` ` ` `print` `(countSubsequences(arr, N, K)) ` ` ` `# This code is contributed by ANKITKUAR34 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count of K ` `// length subsequence whose product ` `// is even ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate nCr ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `if` `(r > n) ` ` ` `return` `0; ` ` ` ` ` `return` `fact(n) / (fact(r) * ` ` ` `fact(n - r)); ` `} ` ` ` `// Returns factorial of n ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` ` ` `return` `res; ` `} ` ` ` `// Function for finding number ` `// of K length subsequences ` `// whose product is even number ` `static` `int` `countSubsequences(` `int` `[]arr, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `countOdd = 0; ` ` ` ` ` `// Counting odd numbers in the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % 2 == 1) ` ` ` `countOdd++; ` ` ` `} ` ` ` `int` `ans = nCr(n, k) - nCr(countOdd, k); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]arr = { 2, 4 }; ` ` ` `int` `K = 1; ` ` ` ` ` `int` `N = arr.Length; ` ` ` ` ` `Console.WriteLine(countSubsequences(arr, N, K)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

2

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:

- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Count of all subsequence whose product is a Composite number
- Count arrays of length K whose product of elements is same as that of given array
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subsequence whose XOR value is odd
- Smallest Palindromic Subsequence of Even Length in Range [L, R]
- Length of longest increasing absolute even subsequence
- Maximum even sum subsequence of length K
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Find the Increasing subsequence of length three with maximum product
- Check if a number has an odd count of odd divisors and even count of even divisors
- Number of N length sequences whose product is M
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count ways to express even number ānā as sum of even integers
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers from given range having odd digits at odd places and even digits at even places
- Count pairs in a sorted array whose product is less than k

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.