Given an array arr[] consisting of N positive integers, the task is to count the number of subarrays whose Bitwise OR of its elements even.
Examples:
Input: arr[] = {1, 5, 4, 2, 6 }
Output: 6
Explanation:
The subarrays with even Bitwise OR are {4}, {2}, {6}, {2, 6}, {4, 2}, {4, 2, 6}.
Therefore, the number of subarrays having even Bitwise OR are 6.
Input: arr[] ={2, 5, 6, 8}
Output: 4
Naive Approach: The simplest approach to solve the problem is to generate all subarrays and if the Bitwise OR of any subarray is even, then increase the count of such subarrays. After checking for all the subarrays, print the count obtained as the result.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing the fact that if any of the elements in the subarray is odd, then it will surely make the Bitwise OR of the subarray odd. Therefore, the idea is to find the length of the continuous segment in the array which is even, and add its contribution to the total count.
Follow the steps below to solve the problem:
- Initialize a variable, say, count, to store the total number of subarrays having Bitwise OR as even.
- Initialize a variable, say L, to store the count of adjacent elements that are even.
- Traverse the given array arr[] and perform the following steps:
- If the value of L is non-zero, then add L*(L + 1)/2 to the variable count.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static 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;
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 4 , 2 , 6 };
int N = arr.length;
System.out.print(bitOr(arr, N));
}
}
|
Python3
def bitOr(arr, N):
count = 0
length = 0
for i in range (N):
if (arr[i] % 2 = = 0 ):
length + = 1
else :
if (length ! = 0 ):
count + = ((length) * (length + 1 )) / / 2
length = 0
count + = ((length) * (length + 1 )) / / 2
return count
if __name__ = = '__main__' :
arr = [ 1 , 5 , 4 , 2 , 6 ]
N = len (arr)
print (bitOr(arr, N))
|
C#
using System;
class GFG
{
static 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;
}
static void Main()
{
int [] arr = { 1, 5, 4, 2, 6 };
int N = arr.Length;
Console.Write(bitOr(arr, N));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)