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

📅  最后修改于: 2022-05-13 01:56:09.603000             🧑  作者: Mango

计算数组中具有奇数位或值的子序列

给定一个由N个正整数组成的数组arr[] ,任务是从给定数组中找出位或值为奇数的子序列的数量。

例子:

朴素方法:解决问题的最简单方法是生成给定数组的所有子序列,并为每个子序列检查其按位或值是否为奇数。如果是奇数,则将计数加一。检查所有子序列后,打印获得的计数。
时间复杂度: O(N*2^N)
辅助空间: O(1)

有效方法:给定问题可以通过观察对于具有奇数位或值的子序列来解决,至少该子序列的一个元素应该是奇数。因此,子序列中至少有一个元素的最低有效位应等于 1。请按照以下步骤解决此问题:

  • 将数组 arr []中存在的偶数和奇数元素的计数分别存储在偶数奇数变量中。
  • 使用变量i遍历数组A[]
    • 如果A[i]的值为奇数,则将奇数的值增加1
    • 否则,将even的值增加1
  • 至少有一个奇数元素和任意数量的偶数元素的总组合可以由以下公式给出:[2^(奇数元素) – 1] * 2^(偶数元素)。由于至少需要一个奇数元素,因此排除了 1 的空组合

下面是该方法的实现:

C++
// C++ implementation for the above approach
#include 
 
using namespace std;
 
// Function to count the subsequences
// having odd bitwise OR value
int countSubsequences(vector arr)
{
    // Stores count of odd elements
    int odd = 0;
 
    // Stores count of even elements
    int even = 0;
 
    // Traverse the array arr[]
    for (int x : arr) {
 
        // If element is odd
        if (x & 1)
            odd++;
        else
            even++;
    }
 
    // Return the final answer
    return ((1 << odd) - 1) *
              (1 << even);
}
 
// Driver Code
int main()
{
    // Given array arr[]
    vector arr = {2, 4, 1};
   
    cout << countSubsequences(arr);
}


Java
// Java implementation for the above approach
import java.io.*;
 
class GFG {
   
      // Function to count the subsequences
    // having odd bitwise OR value
    static int countSubsequences(int arr[])
    {
       
        // Stores count of odd elements
        int odd = 0;
 
        // Stores count of even elements
        int even = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // If element is odd
            if ((arr[i] & 1) != 0)
                odd++;
            else
                even++;
        }
 
        // Return the final answer
        return ((1 << odd) - 1) *
                  (1 << even);
    }
 
    // Driver Code
    public static void main (String[] args) {
        // Given array arr[]
        int arr[] = {2, 4, 1};
   
        System.out.println(countSubsequences(arr));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 implementation for the above approach
 
# Function to count the subsequences
# having odd bitwise OR value
def countSubsequences(arr) :
 
    # Stores count of odd elements
    odd = 0;
 
    # Stores count of even elements
    even = 0;
 
    # Traverse the array arr[]
    for x in arr:
 
        # If element is odd
        if (x & 1) :
            odd += 1;
        else :
            even += 1;
     
    # Return the final answer
    return ((1 << odd) - 1) * (1 << even);
 
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array arr[]
    arr = [2, 4, 1];
   
    print(countSubsequences(arr));
     
    # This code is contributed by AnkThon


C#
// Java implementation for the above approach
using System;
 
class GFG {
   
      // Function to count the subsequences
    // having odd bitwise OR value
    static int countSubsequences(int []arr)
    {
       
        // Stores count of odd elements
        int odd = 0;
 
        // Stores count of even elements
        int even = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < arr.Length; i++) {
 
            // If element is odd
            if ((arr[i] & 1) != 0)
                odd++;
            else
                even++;
        }
 
        // Return the final answer
        return ((1 << odd) - 1) *
                  (1 << even);
    }
 
    // Driver Code
    public static void Main (String[] args) {
        // Given array arr[]
        int []arr = {2, 4, 1};
   
        Console.Write(countSubsequences(arr));
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript



输出
4

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