给定大小为N的数组arr [] ,任务是计算给定数组中具有奇数按位XOR值的子数组的数量。
例子:
Input: arr[] = {1, 4, 7, 9, 10}
Output: 8
Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 4}, {1, 4, 7, 9}, {1, 4, 7, 9, 10}, {7}, {9}, {4, 7}, {9, 10}.
Input: arr[] ={1, 5, 6}
Output: 3
Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 5}, {1, 5, 6}.
天真的方法:解决此问题的最简单方法是检查每个子数组的按位XOR是否为奇数。如果发现是奇数,则增加计数。最后,将计数打印为结果。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,该想法基于以下观察结果:仅当子数组中奇数元素的数量为奇数时,子数组的按位XOR才为奇数。要解决该问题,请找到从索引0开始并满足给定条件的子数组的数量。然后,遍历数组并更新从索引i开始的满足给定条件的子数组的数量。请按照以下步骤解决问题:
- 将变量Odd , C_odd初始化为0 ,以分别存储直到第i个索引的奇数个数和从第i个索引开始的所需子数组的个数。
- 使用变量i遍历数组arr []并检查以下步骤:
- 如果arr [i]的值是奇数,则将Odd的值更新为!Odd 。
- 如果Odd的值不为零,则将C_odd递增1 。
- 再次,使用变量i遍历数组arr []并执行以下操作:
- 将C_odd的值添加到变量res 。
- 如果arr [i]的值是奇数,则将C_odd的值更新为(N – i – C_odd) 。
- 完成上述步骤后,打印res的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
void oddXorSubarray(int a[], int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having odd
// Bitwise XOR values starting at 0-th index
for (int i = 0; i < n; i++) {
// Check if current element is odd
if (a[i] & 1) {
odd = !odd;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd) {
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for (int i = 0; i < n; i++) {
// Add c_odd to result
result += c_odd;
if (a[i] & 1) {
c_odd = (n - i - c_odd);
}
}
// Print the result
cout << result;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = sizeof(arr) / sizeof(arr[0]);
oddXorSubarray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
static void oddXorSubarray(int a[], int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having odd
// Bitwise XOR values starting at 0-th index
for (int i = 0; i < n; i++)
{
// Check if current element is odd
if (a[i] % 2 != 0)
{
odd = (odd == 0) ? 1 : 0;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd != 0)
{
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for (int i = 0; i < n; i++)
{
// Add c_odd to result
result += c_odd;
if (a[i] % 2 != 0)
{
c_odd = (n - i - c_odd);
}
}
// Print the result
System.out.println(result);
}
// Driver Code
public static void main (String[] args)
{
// Given array
int arr[] = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = arr.length;
oddXorSubarray(arr, N);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to count the number of subarrays
# of the given array having odd Bitwise XOR
def oddXorSubarray(a, n):
# Stores number of odd
# numbers upto i-th index
odd = 0
# Stores number of required
# subarrays starting from i-th index
c_odd = 0
# Store the required result
result = 0
# Find the number of subarrays having odd
# Bitwise XOR values starting at 0-th index
for i in range(n):
# Check if current element is odd
if (a[i] & 1):
odd = not odd
# If the current value of odd is not
# zero, increment c_odd by 1
if (odd):
c_odd += 1
# Find the number of subarrays having odd
# bitwise XOR value starting at ith index
# and add to result
for i in range(n):
# Add c_odd to result
result += c_odd
if (a[i] & 1):
c_odd = (n - i - c_odd)
# Prthe result
print (result)
# Driver Code
if __name__ == '__main__':
# Given array
arr = [1, 4, 7, 9, 10]
# Stores the size of the array
N = len(arr)
oddXorSubarray(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
// of the given array having odd Bitwise XOR
static void oddXorSubarray(int []a, int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having
// odd Bitwise XOR values starting at
// 0-th index
for(int i = 0; i < n; i++)
{
// Check if current element is odd
if (a[i] % 2 != 0)
{
odd = (odd == 0) ? 1 : 0;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd != 0)
{
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for(int i = 0; i < n; i++)
{
// Add c_odd to result
result += c_odd;
if (a[i] % 2 != 0)
{
c_odd = (n - i - c_odd);
}
}
// Print the result
Console.WriteLine(result);
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = arr.Length;
oddXorSubarray(arr, N);
}
}
// This code is contributed by 29AjayKumar
输出:
8
时间复杂度: O(N)
辅助空间: O(1)