# Count ways to make product of array elements even by replacements

• Difficulty Level : Hard
• Last Updated : 27 Apr, 2021

Given an array arr[] consisting of N integers, the task is to count the number of ways to make the product of array elements even by replacing array elements any number of times. Since the count can be very large, print the count modulo 109+7.

Examples:

Input: arr[] = {1, 3}
Output: 3
Explanation:
Operation 1: Replacing arr[0] by 2. Therefore, arr[] modifies to {2, 3}. Product = 6.
Operation 2: Replacing arr[0] by 10. Therefore, arr[] modifies to {1, 10}, Product= 10.
Operation 3: Replacing arr[0] and arr[1] by 2. Therefore, arr[] modifies to {2, 2}, Product = 4.
Hence, 3 possible ways exists.

Input: arr[] = {3}
Output:

Approach: The idea is to use Greedy Approach to solve this problem.
Follow the steps below to solve the problem:

• In order to make the product of the array even, at least one even array element must exist.
• Traverse the array. For every array element, the following two situations arise:
• If the array consists of a single element only, then only a single way exists to make the product of the array even.
• Otherwise, 2N – 1 ways.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count ways to make``// product of given array even.``void` `makeProductEven(``int` `arr[], ``int` `N)``{``    ``int` `m = 1000000007, ans = 1;` `    ``// Calculate 2 ^ N``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``        ``ans = (ans * 2) % m;``    ``}` `    ``// Print the answer``    ``cout << ans - 1;``}` `// Driver Code``int` `main()``{``  ` `    ``// Given array``    ``int` `arr[] = { 1, 3 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``makeProductEven(arr, N);``    ``return` `0;``}`

## Java

 `// Java program for above approach``/*package whatever //do not write package name here */``import` `java.io.*;``class` `GFG``{` `  ``// Method to count ways to make``  ``// product of given array even.``  ``static` `void` `makeProductEven(``int` `arr[], ``int` `N)``  ``{``    ``int` `m = ``1000000007``, ans = ``1``;` `    ``// Calculate 2 ^ N``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{``      ``ans = (ans * ``2``) % m;``    ``}` `    ``// Print the answer``    ``System.out.println(ans - ``1``);``  ``}``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``// Given array``    ``int` `arr[] = { ``1``, ``3` `};` `    ``// Size of the array``    ``int` `N = arr.length;``    ``makeProductEven(arr, N);``  ``}``}` `// This code is contributed by shubham agrawal`

## Python3

 `# Python3 program for the above approach` `# Function to count ways to make``# product of given array even.``def` `makeProductEven(arr, N) :``    ``m ``=` `1000000007``; ans ``=` `1``;` `    ``# Calculate 2 ^ N``    ``for` `i ``in` `range``(N) :``        ``ans ``=` `(ans ``*` `2``) ``%` `m;` `    ``# Print the answer``    ``print``(ans ``-` `1``);` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# Given array``    ``arr ``=` `[ ``1``, ``3` `];` `    ``# Size of the array``    ``N ``=` `len``(arr);` `    ``makeProductEven(arr, N);` `    ``# This code is contributed by AnkThon`

## C#

 `// C# program for above approach``/*package whatever //do not write package name here */``using` `System;``public` `class` `GFG``{` `  ``// Method to count ways to make``  ``// product of given array even.``  ``static` `void` `makeProductEven(``int` `[]arr, ``int` `N)``  ``{``    ``int` `m = 1000000007, ans = 1;` `    ``// Calculate 2 ^ N``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``      ``ans = (ans * 2) % m;``    ``}` `    ``// Print the answer``    ``Console.WriteLine(ans - 1);``  ``}``  ` `  ``// Driver code``  ``public` `static` `void` `Main(String[] args)``  ``{` `    ``// Given array``    ``int` `[]arr = { 1, 3 };` `    ``// Size of the array``    ``int` `N = arr.Length;``    ``makeProductEven(arr, N);``  ``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output
`3`

Time Complexity: O(N)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up