I tried but I am not able to correctly implement it. This is the code I wrote which is wrong as I am not counting some of the subarrays. Is it possible to write an algorithm which runs is O(n)
int even=0 , count =0 ;
for( int i=0 ; i<n ; ++i ) {
if(arr[i]%4==0) {
count = count + n - i ;
if(even==1) {
count = count + n - i ;
--even;
}
}
else if(a[i]%2==0) {
++even;
if(even==2) {
count = count + n - i ;
--even;
}
}
}
Sample I/O - if arr[] = {1,4,9} then answer should be 4 as {1,4} , {1,4,9} , {4,9} , {4} have their products divisible by 4 .