Count of K length subsequence whose product is even

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.


Input: arr[] = [2, 3, 1, 7], K = 3
Output: 3
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 = 1
Output: 2
There are 2 subsequence of length 1 whose product is even number {2} {4}.


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.

  1. For making a product of the subsequence odd we must choose K numbers as odd.
  2. So the number of subsequences of length K whose product is odd is possibly finding k odd numbers, i.e., “o choose k” or   _{k}^{o}\textrm{C}
    where o is the count of odd numbers in the subsequence.
  3. \text{So count of a subsequence with even product  = }  _{k}^{n}\textrm{C} -  _{k}^{o}\textrm{C}
    where n and o is the count of total numbers and odd numbers respectively.

Below is the implementation of above program:





// 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)
    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;




Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.