Count subarrays having even Bitwise OR | 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
function bitOr ( arr , N ) { var count = 0 ; var length = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += Math . floor ( ( length ) * ( length + 1 ) / 2 ) ; } length = 0 ; } } count += Math . floor ( ( length ) * ( length + 1 ) / 2 ) ; return count ; } var arr = [ 1 , 5 , 4 , 2 , 6 ] var N = arr . length ; document . write ( bitOr ( arr , N ) ) ;