Number of subarrays having even product
Given an array arr[] consisting of N integers, the task is to count the total number of subarrays having even product.
Examples :
Input: arr[] = { 7, 5, 4, 9 }
Output: 6
Explanation: There are total 6 subarrays
- { 4 }
- { 5, 4 }
- { 7, 5, 4 }
- { 7, 5, 4, 9 }
- { 5, 4, 9 }
- { 4, 9 }
Input: arr[] = { 1, 3, 5 }
Output: 0
Naive Approach: The simplest approach to solve this problem is to generate all possible subarrays from the given array and for each subarray, check if its product is even or not. If found to be true for any subarray, increase count. Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void evenproduct( int arr[], int length)
{
int count = 0;
for ( int i = 0; i < length+1; i++) {
int product = 1;
for ( int j = i; j < length+1; j++) {
product *= arr[j];
if (product % 2 == 0)
++count;
}
}
cout<<count;
}
int main()
{
int arr[] = { 7, 5, 4, 9 };
int length = ( sizeof (arr)/ sizeof (arr[0]))- 1;
evenproduct(arr, length);
}
|
Java
import java.io.*;
class GFG {
static void evenproduct( int arr[], int length)
{
int count = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
int product = 1 ;
for ( int j = i; j < arr.length; j++) {
product *= arr[j];
if (product % 2 == 0 )
++count;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int arr[] = { 7 , 5 , 4 , 9 };
int length = arr.length - 1 ;
evenproduct(arr, length);
}
}
|
Python3
def evenproduct(arr, length) :
count = 0 ;
for i in range (length + 1 ) :
product = 1 ;
for j in range (i, length + 1 ) :
product * = arr[j];
if (product % 2 = = 0 ) :
count + = 1 ;
print (count);
if __name__ = = "__main__" :
arr = [ 7 , 5 , 4 , 9 ];
length = len (arr) - 1 ;
evenproduct(arr, length);
|
C#
using System;
public class GFG
{
static void evenproduct( int []arr, int length)
{
int count = 0;
for ( int i = 0; i < arr.Length; i++) {
int product = 1;
for ( int j = i; j < arr.Length; j++) {
product *= arr[j];
if (product % 2 == 0)
++count;
}
}
Console.WriteLine(count);
}
public static void Main( string [] args)
{
int []arr = { 7, 5, 4, 9 };
int length = arr.Length - 1;
evenproduct(arr, length);
}
}
|
Javascript
<script>
function evenproduct(arr, length)
{
var count = 0;
var i,j;
for (i = 0; i < length+1; i++) {
var product = 1;
for (j = i; j < length+1; j++) {
product *= arr[j];
if (product % 2 == 0)
++count;
}
}
document.write(count);
}
var arr = [7, 5, 4, 9];
var length = arr.length;
evenproduct(arr, length);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to optimize the above approach:
- The total number of subarrays in an array of size N is N * (N + 1) / 2.
- The count of subarrays with an odd product is equal to the total number of continuous odd elements present in the array.
- Therefore, count of subarrays with even product = (Total number of subarrays – Subarrays with the odd product).
- Print the obtained value of the count of subarrays.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void evenproduct( int arr[],
int length)
{
int total_subarray
= length * (length + 1) / 2;
int total_odd = 0;
int count_odd = 0;
for ( int i = 0; i < length; ++i) {
if (arr[i] % 2 == 0) {
count_odd = 0;
}
else {
++count_odd;
total_odd += count_odd;
}
}
cout << (total_subarray
- total_odd) << endl;
}
int main()
{
int arr[] = { 7, 5, 4, 9 };
int length = sizeof (arr) / sizeof (arr[0]);
evenproduct(arr, length);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void evenproduct( int arr[],
int length)
{
int total_subarray
= length * (length + 1 ) / 2 ;
int total_odd = 0 ;
int count_odd = 0 ;
for ( int i = 0 ; i < arr.length; ++i) {
if (arr[i] % 2 == 0 ) {
count_odd = 0 ;
}
else {
++count_odd;
total_odd += count_odd;
}
}
System.out.println(total_subarray
- total_odd);
}
public static void main(String[] args)
{
int arr[] = { 7 , 5 , 4 , 9 };
int length = arr.length;
evenproduct(arr, length);
}
}
|
Python3
def evenproduct(arr, length):
total_subarray = length * (length + 1 ) / / 2
total_odd = 0
count_odd = 0
for i in range (length):
if (arr[i] % 2 = = 0 ):
count_odd = 0
else :
count_odd + = 1
total_odd + = count_odd
print (total_subarray
- total_odd)
if __name__ = = "__main__" :
arr = [ 7 , 5 , 4 , 9 ]
length = len (arr)
evenproduct(arr, length)
|
C#
using System;
class GFG
{
static void evenproduct( int [] arr,
int length)
{
int total_subarray
= length * (length + 1) / 2;
int total_odd = 0;
int count_odd = 0;
for ( int i = 0; i < arr.Length; ++i) {
if (arr[i] % 2 == 0) {
count_odd = 0;
}
else {
++count_odd;
total_odd += count_odd;
}
}
Console.WriteLine(total_subarray
- total_odd);
}
public static void Main( string [] args)
{
int [] arr = { 7, 5, 4, 9 };
int length = arr.Length;
evenproduct(arr, length);
}
}
|
Javascript
<script>
function evenproduct(arr, length)
{
var total_subarray
= length * (length + 1) / 2;
var total_odd = 0;
var count_odd = 0;
for (i = 0; i < arr.length; ++i) {
if (arr[i] % 2 == 0) {
count_odd = 0;
}
else {
++count_odd;
total_odd += count_odd;
}
}
document.write(total_subarray
- total_odd);
}
var arr = [ 7, 5, 4, 9 ];
var length = arr.length;
evenproduct(arr, length);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
04 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...