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;
// Function to find number of
// subarrays with even OR
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 last odd element is not present in current
// subarray
if (j == -1 || j < (i - K + 1))
ans++;
}
}
return ans;
}
// Driver Code
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
// Java Implementation
public class CountEvenOrSubarrays {
// Function to find the number of subarrays with even OR
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 the last odd element is not present in the current subarray
if (j == -1 || j < (i - K + 1)) {
ans++;
}
}
}
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by Sakshi
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 the last odd element is not present in the current subarray
if j == -1 or j < (i - K + 1):
ans += 1
return ans
# Driver Code
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
{
// Function to find number of
// subarrays with even OR
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 last odd element is not present in the current
// subarray
if (j == -1 || j < (i - K + 1))
ans++;
}
}
return ans;
}
// Driver Code
static void Main()
{
int K = 3;
int[] arr = { 4, 2, 6, 7, 8 };
int N = arr.Length;
Console.WriteLine(CountEvenOr(arr, N, K));
// Wait for user input before closing the console window
Console.ReadLine();
}
}
JavaScript
// Javascript Implementation
// Function to find number of subarrays with even OR
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 last odd element is not present in current subarray
if (j === -1 || j < (i - K + 1)) {
ans++;
}
}
}
return ans;
}
// Driver Code
const K = 3;
const arr = [4, 2, 6, 7, 8];
const N = arr.length;
console.log(countEvenOr(arr, N, K));
// This code is contributed by Tapesh(tapeshdua420)
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Number of subarrays have bitwise OR >= K Given an array arr[] and an integer K, the task is to count the number of sub-arrays having bitwise OR ? K. Examples: Input: arr[] = { 1, 2, 3 } K = 3 Output: 4Bitwise OR of sub-arrays: { 1 } = 1 { 1, 2 } = 3 { 1, 2, 3 } = 3 { 2 } = 2 { 2, 3 } = 3 { 3 } = 3 4 sub-arrays have bitwise OR ? K Input: ar
15+ min read
Bitwise operations on Subarrays of size K Given an array arr[] of positive integers and a number K, the task is to find the minimum and maximum values of Bitwise operation on elements of subarray of size K. Examples: Input: arr[]={2, 5, 3, 6, 11, 13}, k = 3 Output: Maximum AND = 2 Minimum AND = 0 Maximum OR = 15 Minimum OR = 7 Explanation:
15+ min read
Number of subarrays with given product Given an array of positive numbers and a number k, find the number of subarrays having product exactly equal to k. We may assume that there is no overflow. Examples : Input : arr = [2, 1, 1, 1, 4, 5] k = 4 Output : 4 1st subarray : arr[1..4] 2nd subarray : arr[2..4] 3rd subarray : arr[3..4] 4th suba
15 min read
Maximum even numbers present in any subarray of size K Given an array arr[] of size N and an integer K, the task is to find the maximum number of even numbers present in any subarray of size K. Examples: Input: arr[] = {2, 3, 5, 4, 7, 6}, K = 3 Output: 2 Explanation: Subarrays of size K(=3) with maximum count of even numbers are { arr[3], arr[4], arr[5]
12 min read
Find maximum product of Bitwise AND and Bitwise OR of K-size subarray Given an array arr[] containing N integers and an integer K, the task is to find the maximum value of the product of Bitwise AND and Bitwise OR of all elements of a K-sized subarray. Example: Input: arr[] = {1, 2, 3, 4}, K = 2Output: 6Explanation: Bitwise AND and Bitwise XOR of all K-sized subarrays
9 min read