📜  具有最大可能 XOR 值的子集计数

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

具有最大可能 XOR 值的子集计数

给定一个由N个正整数组成的数组arr[] 。任务是计算具有最大按位XORarr[]的不同非空子集的数量。

例子:

方法:这个问题可以通过使用Bit Masking来解决。请按照以下步骤解决给定的问题。

  • 初始化一个变量maxXorVal = 0 ,以将子集的最大可能按位异或存储在 arr[] 中。
  • 遍历数组arr[]以找到maxXorVal的值。
  • 初始化一个变量说countSubsets = 0 ,以计算具有最大按位异或的子集的数量。
  • 之后计算值为maxXorVal的子集的数量。
  • 返回countSubsets作为最终答案。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find subsets having maximum XOR
int countMaxOrSubsets(vector& nums)
{
    // Store the size of arr[]
    long long n = nums.size();
 
    // To store maximum possible
    // bitwise XOR subset in arr[]
    long long maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
 
        long long xorVal = 0;
        for (int j = 0; j < n; j++) {
 
            if (i & (1 << j)) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long long count = 0;
 
    for (int i = 0; i < (1 << n); i++) {
        long long val = 0;
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal) {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
    vector arr = { 3, 2, 1, 5 };
 
    // Print the answer
    cout << countMaxOrSubsets(arr);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
 
public class GFG
{
   
// Function to find subsets having maximum XOR
static int countMaxOrSubsets(int []nums)
{
    // Store the size of arr[]
    long n = nums.length;
 
    // To store maximum possible
    // bitwise XOR subset in arr[]
    long maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
 
        long xorVal = 0;
        for (int j = 0; j < n; j++) {
 
            if ((i & (1 << j)) == 0) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = Math.max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long count = 0;
 
    for (int i = 0; i < (1 << n); i++) {
        long val = 0;
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) == 0) {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal) {
            count++;
        }
    }
    return (int)count;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 4;
    int []arr = { 3, 2, 1, 5 };
 
    // Print the answer
    System.out.print(countMaxOrSubsets(arr));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for above approach
 
# Function to find subsets having maximum XOR
def countMaxOrSubsets(nums):
 
    # Store the size of arr[]
    n = len(nums)
 
    # To store maximum possible
    # bitwise XOR subset in arr[]
    maxXorVal = 0
 
    # Find the maximum bitwise xor value
    for i in range(0, (1 << n)):
 
        xorVal = 0
        for j in range(0, n):
 
            if (i & (1 << j)):
                xorVal = (xorVal ^ nums[j])
 
        # Take maximum of each value
        maxXorVal = max(maxXorVal, xorVal)
 
    # Count the number
    # of subsets having bitwise
    # XOR value as maxXorVal
    count = 0
 
    for i in range(0, (1 << n)):
        val = 0
        for j in range(0, n):
            if (i & (1 << j)):
                val = (val ^ nums[j])
 
        if (val == maxXorVal):
            count += 1
    return count
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    arr = [3, 2, 1, 5]
 
    # Print the answer
    print(countMaxOrSubsets(arr))
 
# This code is contributed by rakeshsahni


C#
// C# program for above approach
using System;
 
public class GFG
{
   
// Function to find subsets having maximum XOR
static int countMaxOrSubsets(int []nums)
{
   
    // Store the size of []arr
    int n = nums.Length;
 
    // To store maximum possible
    // bitwise XOR subset in []arr
    int maxXorVal = 0;
 
    // Find the maximum bitwise xor value
    for (int i = 0; i < (1 << n); i++) {
 
        long xorVal = 0;
        for (int j = 0; j < n; j++) {
 
            if ((i & (1 << j)) == 0) {
                xorVal = (xorVal ^ nums[j]);
            }
        }
 
        // Take maximum of each value
        maxXorVal = (int)Math.Max(maxXorVal, xorVal);
    }
 
    // Count the number
    // of subsets having bitwise
    // XOR value as maxXorVal
    long count = 0;
 
    for (int i = 0; i < (1 << n); i++) {
        long val = 0;
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) == 0) {
                val = (val ^ nums[j]);
            }
        }
 
        if (val == maxXorVal) {
            count++;
        }
    }
    return (int)count;
}
 
// Driver Code
public static void Main(String []args)
{
    int []arr = { 3, 2, 1, 5 };
 
    // Print the answer
    Console.Write(countMaxOrSubsets(arr));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
2

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