📜  计数甚至具有按位或的子数组

📅  最后修改于: 2021-04-17 16:11:14             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是计算其元素的按位或为偶数的子数组的数量。

例子:

天真的方法:解决问题的最简单方法是生成所有子数组,如果任何子数组的按位或为偶数,则增加此类子数组的数量。检查所有子数组后,打印获得的计数作为结果。

时间复杂度: O(N 3 )
辅助空间: O(1)

高效方法:可以通过观察以下事实来优化上述方法:如果子数组中的任何元素为奇数,则必定会使子数组的按位或为奇数。因此,该想法是找到数组中连续段的长度,该长度是偶数,并将其对总计数的贡献相加。
请按照以下步骤解决问题:

  • 初始化一个变量,例如count ,以存储具有按位或为偶数的子数组的总数。
  • 初始化一个变量L ,以存储偶数个相邻元素的计数。
  • 遍历给定数组arr []并执行以下步骤:
    • 如果当前元素是偶数,则将L的值增加1
    • 否则,将值L *(L +1)/ 2添加到变量count并将L更新为0
  • 如果L的值不为零,则将L *(L + 1)/ 2加到变量count上
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// subarrays having even Bitwise OR
int bitOr(int arr[], int N)
{
    // Store number of subarrays
    // having even bitwise OR
    int count = 0;
 
    // Store the length of the current
    // subarray having even numbers
    int length = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If the element is even
        if (arr[i] % 2 == 0) {
 
            // Increment size of the
            // current continuous sequence
// of even array elements
            length++;
        }
 
        // If arr[i] is odd
        else {
 
            // If length is non zero
            if (length != 0) {
 
                // Adding contribution of
                // subarrays consisting
                // only of even numbers
                count += ((length)
                          * (length + 1))
                         / 2;
            }
 
            // Make length of subarray as 0
            length = 0;
        }
    }
 
    // Add contribution of previous subarray
    count += ((length) * (length + 1)) / 2;
 
    // Return total count of subarrays
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 4, 2, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << bitOr(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to count the number of
// subarrays having even Bitwise OR
static int bitOr(int arr[], int N)
{
     
    // Store number of subarrays
    // having even bitwise OR
    int count = 0;
 
    // Store the length of the current
    // subarray having even numbers
    int length = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If the element is even
        if (arr[i] % 2 == 0)
        {
             
            // Increment size of the
            // current continuous sequence
            // of even array elements
            length++;
        }
 
        // If arr[i] is odd
        else
        {
             
            // If length is non zero
            if (length != 0)
            {
                 
                // Adding contribution of
                // subarrays consisting
                // only of even numbers
                count += ((length) * (length + 1)) / 2;
            }
 
            // Make length of subarray as 0
            length = 0;
        }
    }
 
    // Add contribution of previous subarray
    count += ((length) * (length + 1)) / 2;
 
    // Return total count of subarrays
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 5, 4, 2, 6 };
    int N = arr.length;
 
    // Function Call
    System.out.print(bitOr(arr, N));
}
}
 
// This code is contributed by splevel62


Python3
# Python3 program for the above approach
 
# Function to count the number of
# subarrays having even Bitwise OR
def bitOr(arr, N):
     
    # Store number of subarrays
    # having even bitwise OR
    count = 0
 
    # Store the length of the current
    # subarray having even numbers
    length = 0
 
    # Traverse the array
    for i in range(N):
 
        # If the element is even
        if (arr[i] % 2 == 0):
 
            # Increment size of the
            # current continuous sequence
            # of even array elements
            length += 1
             
        # If arr[i] is odd
        else:
             
            # If length is non zero
            if (length != 0):
 
                # Adding contribution of
                # subarrays consisting
                # only of even numbers
                count += ((length) * (length + 1)) // 2
 
            # Make length of subarray as 0
            length = 0
 
    # Add contribution of previous subarray
    count += ((length) * (length + 1)) // 2
 
    # Return total count of subarrays
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 5, 4, 2, 6 ]
    N = len(arr)
 
    # Function Call
    print (bitOr(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to count the number of
  // subarrays having even Bitwise OR
  static int bitOr(int[] arr, int N)
  {
 
    // Store number of subarrays
    // having even bitwise OR
    int count = 0;
 
    // Store the length of the current
    // subarray having even numbers
    int length = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
 
      // If the element is even
      if (arr[i] % 2 == 0)
      {
 
        // Increment size of the
        // current continuous sequence
        // of even array elements
        length++;
      }
 
      // If arr[i] is odd
      else
      {
 
        // If length is non zero
        if (length != 0)
        {
 
          // Adding contribution of
          // subarrays consisting
          // only of even numbers
          count += ((length) * (length + 1)) / 2;
        }
 
        // Make length of subarray as 0
        length = 0;
      }
    }
 
    // Add contribution of previous subarray
    count += ((length) * (length + 1)) / 2;
 
    // Return total count of subarrays
    return count;
  }
 
  // Driver code
  static void Main()
  {
    int[] arr = { 1, 5, 4, 2, 6 };
    int N = arr.Length;
 
    // Function Call
    Console.Write(bitOr(arr, N));
  }
}
 
// This code is contributed by sanjoy_62.


输出:
6

时间复杂度: O(N)
辅助空间: O(1)