Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find size of largest subset with bitwise AND greater than their bitwise XOR

  • Last Updated : 13 Dec, 2021

Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset.

Example:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 2
Explanation: The subset {2, 3} has the bitwise AND of all elements as 2 while the bitwise XOR of all elements os 1. Hence, bitwise AND > bitwise XOR. Therefore, the required size of the subset is 2 which is the maximum possible. Another example of a valid subset is {4, 5}.

Input: arr[] = {24, 20, 18, 17, 16}
Output: 4

 

Approach: The given problem can be solved by generating all the possible subsets of the given set using a recursive approach and maintaining the value of bitwise AND and bitwise XOR of each and every subset. The required answer will be the maximum size of the subset such that it’s bitwise AND > it’s bitwise XOR.

Below is the implementation of the above approach:

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
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




<script>
 
       // JavaScript Program to implement
       // the above approach
 
       // 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
       function maxSizeSubset(
           arr, N, bitwiseXOR,
           bitwiseAND, i, len = 0)
      {
           // Stores the maximum length of subset
           let ans = Number.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
       let arr = [24, 20, 18, 17, 16];
       let N = arr.length;
 
       document.write(maxSizeSubset(
           arr, N, 0,
           Math.pow(2, 10) - 1, 0))
 
   // This code is contributed by Potta Lokesh
   </script>
Output
4

Time Complexity: O(2N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!