Open In App

Number of subarray’s of K size with Even Bitwise-OR

Last Updated : 10 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

1







Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads