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.
Example:
Input: arr[] = [2, 3, 1, 7], K = 3
Output: 3
Explanation:
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
Explanation:
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 nCrint nCr(int n, int r){ if (r > n) return 0; return fact(n) / (fact(r) * fact(n - r));}// Returns factorial of nint 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 numberint 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 codeint main(){ int arr[] = { 2, 4 }; int K = 1; int N = sizeof(arr) / sizeof(arr[0]); cout << countSubsequences(arr, N, K); return 0;} |
Java
// Java implementation to count of K// length subsequence whose product // is evenimport java.util.*;class GFG{ // Function to calculate nCrstatic int nCr(int n, int r){ if (r > n) return 0; return fact(n) / (fact(r) * fact(n - r));}// Returns factorial of nstatic 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 numberstatic 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 codepublic 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 |
Python3
# Python3 implementation to Count of K# length subsequence whose# Product is even# Function to calculate nCrdef nCr(n, r): if (r > n): return 0 return fact(n) // (fact(r) * fact(n - r))# Returns factorial of ndef 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 numberdef 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 codearr = [ 2, 4 ]K = 1N = len(arr)print(countSubsequences(arr, N, K))# This code is contributed by ANKITKUAR34 |
C#
// C# implementation to count of K// length subsequence whose product // is evenusing System;class GFG{ // Function to calculate nCrstatic int nCr(int n, int r){ if (r > n) return 0; return fact(n) / (fact(r) * fact(n - r));}// Returns factorial of nstatic 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 numberstatic 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 codepublic 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 |
Javascript
<script>// javascript implementation to Count of K// length subsequence whose// Product is even// Function to calculate nCrfunction nCr(n, r){ if (r > n) return 0; return fact(n) / (fact(r) * fact(n - r));}// Returns factorial of nfunction fact(n){ var res = 1; for (var i = 2; i <= n; i++) res = res * i; return res;}// Function for finding number// of K length subsequences// whose product is even numberfunction countSubsequences( arr, n, k){ var countOdd = 0; // counting odd numbers in the array for (var i = 0; i < n; i++) { if (arr[i] & 1) countOdd++; } var ans = nCr(n, k) - nCr(countOdd, k); return ans;}// Driver codevar arr = [ 2, 4 ];var K = 1;var N = arr.length;document.write( countSubsequences(arr, N, K));</script> |
Output:
2
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



