Count distinct ways to replace array elements such that product of the array becomes even
Given an array arr[] consisting of N odd integers, the task is to count the different number of ways to make the product of all array elements even, by repeatedly changing any set of elements to any values. Since the count can be very large, print it to modulo 109 + 7.
Examples:
Input: arr[] = {1, 3}
Output: 3
Explanation: All possible ways to make the product of array elements odd are as follows:
Replace arr[0] by any even integer. The array arr[] modifies to {even, 3}. Therefore, the product of the array = even * 3 = even.
Replace arr[1] by any even integer. The array arr[] modifies to {1, even}. Therefore, the product of the array = 1 * even = even.
Replace arr[0] and arr[1] by even integers. Since both array elements become even, the product of the array becomes even. Therefore, the total number of distinct ways to make the array even is 3.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 31
Approach: The idea to solve the given problem is based on the observation that the product of an array is even only when at least one even element is present in the array. Therefore, the total number of distinct ways can be calculated by the number of distinct subsets of the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define M 1000000007
using namespace std;
long long power( long long x, long long y,
long long p)
{
long long res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int totalOperations( int arr[], int N)
{
long long res = power(2, N, M);
res--;
cout << res;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
totalOperations(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static long M = 1000000007 ;
static long power( long x, long y, long p)
{
long res = 1 ;
while (y > 0 )
{
if ((y & 1 ) > 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int totalOperations( int arr[], int N)
{
long res = power( 2 , N, M);
res--;
System.out.print(res);
return 0 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
totalOperations(arr, N);
}
}
|
Python3
M = 1000000007
def power(x, y, p):
global M
res = 1
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p;
y = y >> 1
x = (x * x) % p
return res
def totalOperations(arr, N):
res = power( 2 , N, M)
res - = 1
print (res)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
totalOperations(arr, N)
|
C#
using System;
class GFG {
static long M = 1000000007;
static long power( long x, long y, long p)
{
long res = 1;
while (y > 0)
{
if ((y & 1) > 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int totalOperations( int [] arr, int N)
{
long res = power(2, N, M);
res--;
Console.Write(res);
return 0;
}
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
totalOperations(arr, N);
}
}
|
Javascript
<script>
let M = 1000000007;
function power(x,y,p)
{
let res = 1;
while (y > 0)
{
if ((y & 1) > 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function totalOperations(arr,N)
{
let res = power(2, N, M);
res--;
document.write(res);
}
let arr=[ 1, 2, 3, 4, 5];
let N = arr.length;
totalOperations(arr, N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
26 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...