Number of subarray’s of K size with Even Bitwise-OR
Last Updated :
10 Jan, 2024
You are given an array arr[] containing n integers and an integer k. Find the number of subarrays of arr with length k whose bitwise OR is even.
Examples:
Input: arr[] = {4, 2, 6, 7, 8} , k = 3
Output: 2
Explanation: There are 3 subarrays of length =3
[4,2,6] with bitwise OR 6
[2,6,7] with bitwise OR 7
[6,7,8] with bitwise OR 15 . 1 subarray has even answer .
Input: arr[] ={2, 6, 7, 4}, k = 3
Output: 0
Explanation: There are 2 subarrays of length =3 and both have odd bitwise OR.
Approach: To solve the problem follow the below steps
Sliding Window :We created a window of the given size (here k) and traversed it through the array . If any one of the element present in the window is odd then the bitwise OR of all the elements in the window will be odd. (Because the first bit of odd in binary representation is 1 and even is 0) .
Steps were take to solve the problem.
- Initialize ans = 0 .
- We can maintain previous index j where arr[j] is odd .
- Now for each window we can check if it contains odd element by looking at j (which holds the index of previous odd element ) .
- At index i (i>=k-1), if we find that j < (i-k+1) that means for this current window ending at index i, there is no odd element and increment our ans .
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countEvenOr( int arr[], int N, int K)
{
int j = -1;
int ans = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] & 1)
j = i;
if (i >= K - 1) {
if (j == -1 || j < (i - K + 1))
ans++;
}
}
return ans;
}
int main()
{
int K = 3;
int arr[] = { 4, 2, 6, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countEvenOr(arr, N, K);
return 0;
}
|
Java
public class CountEvenOrSubarrays {
static int countEvenOr( int [] arr, int N, int K) {
int j = - 1 ;
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 1 ) {
j = i;
}
if (i >= K - 1 ) {
if (j == - 1 || j < (i - K + 1 )) {
ans++;
}
}
}
return ans;
}
public static void main(String[] args) {
int K = 3 ;
int [] arr = { 4 , 2 , 6 , 7 , 8 };
int N = arr.length;
System.out.println(countEvenOr(arr, N, K));
}
}
|
Python3
def count_even_or(arr, N, K):
j = - 1
ans = 0
for i in range (N):
if arr[i] % 2 = = 1 :
j = i
if i > = K - 1 :
if j = = - 1 or j < (i - K + 1 ):
ans + = 1
return ans
def main():
K = 3
arr = [ 4 , 2 , 6 , 7 , 8 ]
N = len (arr)
print (count_even_or(arr, N, K))
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static int CountEvenOr( int [] arr, int N, int K)
{
int j = -1;
int ans = 0;
for ( int i = 0; i < N; i++)
{
if ((arr[i] & 1) == 1)
j = i;
if (i >= K - 1)
{
if (j == -1 || j < (i - K + 1))
ans++;
}
}
return ans;
}
static void Main()
{
int K = 3;
int [] arr = { 4, 2, 6, 7, 8 };
int N = arr.Length;
Console.WriteLine(CountEvenOr(arr, N, K));
Console.ReadLine();
}
}
|
Javascript
function countEvenOr(arr, N, K) {
let j = -1;
let ans = 0;
for (let i = 0; i < N; i++) {
if (arr[i] % 2 !== 0) {
j = i;
}
if (i >= K - 1) {
if (j === -1 || j < (i - K + 1)) {
ans++;
}
}
}
return ans;
}
const K = 3;
const arr = [4, 2, 6, 7, 8];
const N = arr.length;
console.log(countEvenOr(arr, N, K));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...