给定大小为N的数组A [] ,任务是计算按位XOR值为奇数的给定数组的子序列数。
例子:
Input: A[] = { 1, 3, 4 }
Output: 4
Explanation: Subsequences with odd Bitwise XOR are { 1 }, { 3} , { 1, 4 }, { 3, 4 }.
Input: A[] = { 2, 8, 6 }
Output: 0
Explanation: No such subsequences are present in the array.
天真的方法:解决问题的最简单方法是生成给定数组的所有子序列,对于每个子序列,检查其按位XOR值是否为奇数。如果发现是奇数,则将计数增加一。检查所有子序列后,打印获得的计数。
时间复杂度: O(N * 2 N )
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下观察结果:仅当子序列中存在的奇数元素个数为奇数时,子序列才会具有奇数按位XOR。这是因为奇数元素的最低有效位等于1 。因此,最低有效位( 1 ^ 1 ^ 1 …. )的XOR到奇数次的值将设置所形成的新数字的最低有效位。因此,形成的新数字是奇数。
请按照以下步骤解决问题:
- 在偶数和奇数分别存储偶数和奇数元素存在于所述阵列A []计数。
- 使用变量i遍历数组A []
- 如果A [i]的值是奇数,则将奇数的值增加1。
- 否则,将even的值增加1 。
- 如果奇数的值等于0 ,则输出0并返回。
- 除此以外,
- 查找具有奇数个奇数元素的总组合。
- 可以通过( X C 1 + X C 3 +…+ X C (x-(x + 1)%2) )*( M C 0 + M C 1 +…+ M C M )= 2 X-来计算1 * 2 M = 2 X + M-1 = 2 N-1 。
- 因此,打印2 N-1
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the subsequences
// having odd bitwise XOR value
void countSubsequences(vector A)
{
// Stores count of odd elements
int odd = 0;
// Stores count of even elements
int even = 0;
// Traverse the array A[]
for(int el : A)
{
// If el is odd
if (el % 2 == 1)
odd++;
else
even++;
}
// If count of odd elements is 0
if (odd == 0)
cout << (0);
else
cout << (1 << (A.size() - 1));
}
// Driver Code
int main()
{
// Given array A[]
vector A = { 1, 3, 4 };
// Function call to count subsequences
// having odd bitwise XOR value
countSubsequences(A);
}
// This code is contributed by mohit kumar 29
Java
// Java program for
// the above approach
import java.io.*;
class GFG {
// Function to count the subsequences
// having odd bitwise XOR value
public static void countSubsequences(int[] A)
{
// Stores count of odd elements
int odd = 0;
// Stores count of even elements
int even = 0;
// Traverse the array A[]
for (int el : A) {
// If el is odd
if (el % 2 == 1)
odd++;
else
even++;
}
// If count of odd elements is 0
if (odd == 0)
System.out.println(0);
else
System.out.println(1 << (A.length - 1));
}
// Driver Code
public static void main(String[] args)
{
// Given array A[]
int[] A = { 1, 3, 4 };
// Function call to count subsequences
// having odd bitwise XOR value
countSubsequences(A);
}
}
Python3
# Python3 program for the above approach
# Function to count the subsequences
# having odd bitwise XOR value
def countSubsequences(A):
# Stores count of odd elements
odd = 0
# Stores count of even elements
even = 0
# Traverse the array A[]
for el in A:
# If el is odd
if (el % 2 == 1):
odd += 1
else:
even += 1
# If count of odd elements is 0
if (odd == 0):
print(0)
else:
print(1 << len(A) - 1)
# Driver Code
if __name__ == "__main__":
# Given array A[]
A = [1, 3, 4]
# Function call to count subsequences
# having odd bitwise XOR value
countSubsequences(A)
# This code is contributed by ukasp
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count the subsequences
// having odd bitwise XOR value
public static void countSubsequences(int[] A)
{
// Stores count of odd elements
int odd = 0;
// Stores count of even elements
int even = 0;
// Traverse the array A[]
foreach (int el in A) {
// If el is odd
if (el % 2 == 1)
odd++;
else
even++;
}
// If count of odd elements is 0
if (odd == 0)
Console.WriteLine(0);
else
Console.WriteLine(1 << (A.Length - 1));
}
// Driver code
public static void Main(String[] args)
{
// Given array A[]
int[] A = { 1, 3, 4 };
// Function call to count subsequences
// having odd bitwise XOR value
countSubsequences(A);
}
}
// This code is contributed by splevel62.
输出:
4
时间复杂度: O(N)
辅助空间: O(1)