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

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: 2Explanation: 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++
Java
Python3
C#
Javascript
// 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 elementsint 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 Codeint 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 program for the above approachimport 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 elementsstatic 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 Codepublic 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.
# Python Program to implement# the above approachimport 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 elementsdef 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# program for the above approachusing 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 elementsstatic 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 Codepublic 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.
<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>
4
Time Complexity: O(2N)Auxiliary Space: O(1)
target_2
lokeshpotta20
samim2000
sanjoy_62
Bitwise-AND
Bitwise-XOR
subset
Arrays
Bit Magic
Arrays
Bit Magic
subset
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division