📌  相关文章
📜  给定数组中具有奇数位与值的子序列计数

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

给定数组中具有奇数位与值的子序列计数

给定一个包含N个整数的数组arr[] ,任务是找到给定数组的子序列数,使得它们的按位与值为奇数。

例子:

推荐:请先在{IDE}上尝试您的方法,然后再继续解决。

天真的方法:给定的问题 可以通过生成给定数组arr[]的所有子序列来解决,并跟踪子序列的计数,使其按位与值是奇数。

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

有效的方法:上述方法可以通过优化 观察 如果一组整数的位与值是奇数,则该集合的每个元素的最低有效位必须是一个集合位。因此,对于具有奇数位与值的子序列,子序列的所有元素都必须是奇数。使用此观察,可以使用以下步骤解决给定问题:

  • 创建一个变量odd ,它存储给定数组arr[]中奇数的总数。用0初始化它。
  • 如果当前整数是奇数整数,则遍历数组并将奇数的值加1
  • 具有X个元素的数组的非空子序列的计数为2 X – 1 。因此,具有所有奇数元素的非空子序列的数量是2奇数- 1 ,这是所需的答案。

下面是该方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find count of subsequences
// having odd bitwise AND value
int countSubsequences(vector arr)
{
    // Stores count of odd elements
    int odd = 0;
 
    // Traverse the array arr[]
    for (int x : arr) {
 
        // If x is odd increment count
        if (x & 1)
            odd++;
    }
 
    // Return Answer
    return (1 << odd) - 1;
}
 
// Driver Code
int main()
{
    vector arr = { 1, 3, 3 };
 
    // Function Call
    cout << countSubsequences(arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    // Function to find count of subsequences
    // having odd bitwise AND value
    static int countSubsequences(int arr[], int N)
    {
        // Stores count of odd elements
        int odd = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If x is odd increment count
            if ((arr[i] & 1) % 2 == 1)
                odd++;
        }
 
        // Return Answer
        return (1 << odd) - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 3;
 
        int arr[] = { 1, 3, 3 };
        // Function Call
        System.out.println(countSubsequences(arr, N));
    }
}
 
// This code is contributed by dwivediyash


Python3
# python program for the above approach
 
# Function to find count of subsequences
# having odd bitwise AND value
def countSubsequences(arr):
 
    # Stores count of odd elements
    odd = 0
 
    # Traverse the array arr[]
    for x in arr:
 
        # If x is odd increment count
        if (x & 1):
            odd = odd + 1
 
    # Return Answer
    return (1 << odd) - 1
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 3, 3]
 
    # Function Call
    print(countSubsequences(arr))
     
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
    // Function to find count of subsequences
    // having odd bitwise AND value
    static int countSubsequences(int []arr, int N)
    {
       
        // Stores count of odd elements
        int odd = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If x is odd increment count
            if ((arr[i] & 1) % 2 == 1)
                odd++;
        }
 
        // Return Answer
        return (1 << odd) - 1;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 3;
 
        int []arr = { 1, 3, 3 };
         
        // Function Call
        Console.WriteLine(countSubsequences(arr, N));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
7

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