Count subarrays having even Bitwise OR | C ++ program for the above approach ; Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bitOr ( int arr [ ] , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 2 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << bitOr ( arr , N ) ; return 0 ; }