Number of subarrays having even product | Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver code ; Input ; Length of an array ; Function call to count even product subarrays
#include <iostream> NEW_LINE 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 ; }