Find size of largest subset with bitwise AND greater than their bitwise XOR
Last Updated :
17 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int maxSizeSubset(
int * arr, int N, int bitwiseXOR,
int bitwiseAND, int i, int len = 0)
{
int ans = INT_MIN;
if (bitwiseAND > bitwiseXOR) {
ans = len;
}
if (i == N) {
return ans;
}
ans = max(ans, maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i + 1, len));
ans = max(ans,
maxSizeSubset(
arr, N,
(arr[i] ^ bitwiseXOR),
(arr[i] & bitwiseAND), i + 1,
len + 1));
return ans;
}
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
import java.util.*;
class GFG {
static int maxSizeSubset(
int [] arr, int N, int bitwiseXOR,
int bitwiseAND, int i, int len)
{
int ans = Integer.MIN_VALUE;
if (bitwiseAND > bitwiseXOR) {
ans = len;
}
if (i == N) {
return ans;
}
ans = Math.max(ans, maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i + 1 , len));
ans = Math.max(ans,
maxSizeSubset(
arr, N,
(arr[i] ^ bitwiseXOR),
(arr[i] & bitwiseAND), i + 1 ,
len + 1 ));
return ans;
}
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 ));
}
}
|
Python3
import sys
def maxSizeSubset(arr, N, bitwiseXOR,
bitwiseAND, i, len ) :
ans = - sys.maxsize - 1
if (bitwiseAND > bitwiseXOR) :
ans = len
if (i = = N) :
return ans
ans = max (ans, maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i + 1 , len ))
ans = max (ans,
maxSizeSubset(
arr, N,
(arr[i] ^ bitwiseXOR),
(arr[i] & bitwiseAND), i + 1 ,
len + 1 ))
return ans
arr = [ 24 , 20 , 18 , 17 , 16 ]
N = len (arr)
print (maxSizeSubset(arr, N, 0 ,
pow ( 2 , 10 ) - 1 , 0 , 0 ))
|
C#
using System;
class GFG {
static int maxSizeSubset(
int []arr, int N, int bitwiseXOR,
int bitwiseAND, int i, int len)
{
int ans = Int32.MinValue;
if (bitwiseAND > bitwiseXOR) {
ans = len;
}
if (i == N) {
return ans;
}
ans = Math.Max(ans, maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i + 1, len));
ans = Math.Max(ans,
maxSizeSubset(
arr, N,
(arr[i] ^ bitwiseXOR),
(arr[i] & bitwiseAND), i + 1,
len + 1));
return ans;
}
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));
}
}
|
Javascript
<script>
function maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i, len = 0)
{
let ans = Number.MIN_VALUE;
if (bitwiseAND > bitwiseXOR) {
ans = len;
}
if (i == N) {
return ans;
}
ans = Math.max(ans, maxSizeSubset(
arr, N, bitwiseXOR,
bitwiseAND, i + 1, len));
ans = Math.max(ans,
maxSizeSubset(
arr, N,
(arr[i] ^ bitwiseXOR),
(arr[i] & bitwiseAND), i + 1,
len + 1));
return ans;
}
let arr = [24, 20, 18, 17, 16];
let N = arr.length;
document.write(maxSizeSubset(
arr, N, 0,
Math.pow(2, 10) - 1, 0))
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Share your thoughts in the comments
Please Login to comment...