Number of subarrays having even product | CPP implementation of the above approach ; Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product
#include <bits/stdc++.h> NEW_LINE 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 ) ; }