📌  相关文章
📜  查找按位与大于按位异或的最大子集的大小

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

查找按位与大于按位异或的最大子集的大小

给定一个包含 N 个整数的数组arr[] ,任务是找到最大子集的大小,使得子集的所有元素的按位与大于子集的所有元素的按位异或。

例子:

方法:给定问题可以通过使用递归方法生成给定集合的所有可能子集并保持每个子集的按位与和按位异或的值来解决。所需的答案将是子集的最大大小,使得它是按位 AND > 它是按位 XOR。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Recursive function to iterate over all
// the subsets of the given array and return
// the maximum size of subset such that the
// bitwise AND > bitwise OR of all elements
int maxSizeSubset(
    int* arr, int N, int bitwiseXOR,
    int bitwiseAND, int i, int len = 0)
{
    // Stores the maximum length of subset
    int ans = INT_MIN;
 
    // Update ans
    if (bitwiseAND > bitwiseXOR) {
        ans = len;
    }
 
    // Base Case
    if (i == N) {
        return ans;
    }
 
    // Recursive call excluding the
    // ith element of the given array
    ans = max(ans, maxSizeSubset(
                       arr, N, bitwiseXOR,
                       bitwiseAND, i + 1, len));
 
    // Recursive call including the ith element
    // of the given array
    ans = max(ans,
              maxSizeSubset(
                  arr, N,
                  (arr[i] ^ bitwiseXOR),
                  (arr[i] & bitwiseAND), i + 1,
                  len + 1));
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 24, 20, 18, 17, 16 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxSizeSubset(
        arr, N, 0,
        pow(2, 10) - 1, 0);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
// Recursive function to iterate over all
// the subsets of the given array and return
// the maximum size of subset such that the
// bitwise AND > bitwise OR of all elements
static int maxSizeSubset(
    int[] arr, int N, int bitwiseXOR,
    int bitwiseAND, int i, int len)
{
   
    // Stores the maximum length of subset
    int ans = Integer.MIN_VALUE;
 
    // Update ans
    if (bitwiseAND > bitwiseXOR) {
        ans = len;
    }
 
    // Base Case
    if (i == N) {
        return ans;
    }
 
    // Recursive call excluding the
    // ith element of the given array
    ans = Math.max(ans, maxSizeSubset(
                       arr, N, bitwiseXOR,
                       bitwiseAND, i + 1, len));
 
    // Recursive call including the ith element
    // of the given array
    ans = Math.max(ans,
              maxSizeSubset(
                  arr, N,
                  (arr[i] ^ bitwiseXOR),
                  (arr[i] & bitwiseAND), i + 1,
                  len + 1));
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void main (String[] args) {
         
    int arr[] = { 24, 20, 18, 17, 16 };
    int N = arr.length;
 
    System.out.println(maxSizeSubset(arr, N, 0, (int)Math.pow(2, 10) - 1, 0, 0));
}
}
 
// This code is contributed by target_2.


Python3
# Python Program to implement
# the above approach
import sys
 
# Recursive function to iterate over all
# the subsets of the given array and return
# the maximum size of subset such that the
# bitwise AND > bitwise OR of all elements
def maxSizeSubset(arr, N, bitwiseXOR,
                    bitwiseAND, i, len) :
                         
    # Stores the maximum length of subset
    ans = -sys.maxsize - 1
 
    # Update ans
    if (bitwiseAND > bitwiseXOR) :
        ans = len
     
 
    # Base Case
    if (i == N) :
        return ans
     
 
    # Recursive call excluding the
    # ith element of the given array
    ans = max(ans, maxSizeSubset(
                       arr, N, bitwiseXOR,
                       bitwiseAND, i + 1, len))
 
    # Recursive call including the ith element
    # of the given array
    ans = max(ans,
              maxSizeSubset(
                  arr, N,
                  (arr[i] ^ bitwiseXOR),
                  (arr[i] & bitwiseAND), i + 1,
                  len + 1))
 
    # Return Answer
    return ans
 
 
# Driver Code
 
arr = [ 24, 20, 18, 17, 16 ]
N = len(arr)
 
print(maxSizeSubset(arr, N, 0,
            pow(2, 10) - 1, 0, 0))
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
class GFG {
 
// Recursive function to iterate over all
// the subsets of the given array and return
// the maximum size of subset such that the
// bitwise AND > bitwise OR of all elements
static int maxSizeSubset(
    int []arr, int N, int bitwiseXOR,
    int bitwiseAND, int i, int len)
{
   
    // Stores the maximum length of subset
    int ans = Int32.MinValue;
 
    // Update ans
    if (bitwiseAND > bitwiseXOR) {
        ans = len;
    }
 
    // Base Case
    if (i == N) {
        return ans;
    }
 
    // Recursive call excluding the
    // ith element of the given array
    ans = Math.Max(ans, maxSizeSubset(
                       arr, N, bitwiseXOR,
                       bitwiseAND, i + 1, len));
 
    // Recursive call including the ith element
    // of the given array
    ans = Math.Max(ans,
              maxSizeSubset(
                  arr, N,
                  (arr[i] ^ bitwiseXOR),
                  (arr[i] & bitwiseAND), i + 1,
                  len + 1));
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void Main () {
         
    int []arr = { 24, 20, 18, 17, 16 };
    int N = arr.Length;
 
    Console.Write(maxSizeSubset(arr, N, 0, (int)Math.Pow(2, 10) - 1, 0, 0));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
4

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