📌  相关文章
📜  从数组中计算具有奇数位异或值的子序列

📅  最后修改于: 2021-09-05 11:43:20             🧑  作者: Mango

给定一个大小为N的数组A[] ,任务是计算给定数组中按位异或值为奇数的子序列的数量。

例子:

朴素方法:解决问题的最简单方法是生成给定数组的所有子序列,并针对每个子序列检查其按位异或值是否为奇数。如果发现是奇数,则将计数加一。检查所有子序列后,打印获得的计数。

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

高效方法:为了优化上述方法,该想法基于以下观察:只有当其中存在的奇数元素为奇数时,子序列才会具有奇数按位异或。这是因为奇数元素的最低有效位等于1 。因此,将最低有效位 ( 1^1^1…. ) 异或最多奇数次设置新数字的最低有效位。因此,形成的新数是奇数。
请按照以下步骤解决问题:

  1. 将数组A[]中存在的偶数和奇数元素的计数分别存储在偶数奇数中
  2. 使用变量i遍历数组A[]
    • 如果A[i] 的值为奇数,则将奇数的值加1。
    • 否则,将even的值增加1
  3. 如果odd的值等于0 ,则打印0并返回。
  4. 除此以外,
    • 找出具有奇数个奇数元素的总组合。
    • 这可以计算为( 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.


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live