Number of subsets with a given AND value
Given an array arr of length N and an integer X, the task is to find the number of subsets whose AND value is X.
Examples:
Input: arr[] = {2, 3, 2} X = 2
Output: 6
All possible subsets and there AND values are:
{2} = 2
{3} = 3
{2} = 2
{2, 3} = 2 & 3 = 2
{3, 2} = 3 & 2 = 2
{2, 2} = 2 & 2 = 2
{2, 3, 2} = 2 & 3 & 2 = 2
Input: arr[] = {0, 0, 0}, X = 0
Output: 7
Approach: A simple approach is to solve the problem by generating all the possible subsets and then by counting the number of subsets with the given AND value. However, for smaller values of array elements, it can be solved using dynamic programming.
Let’s look at the recurrence relation first.
dp[i][curr_and] = dp[i + 1][curr_and] + dp[i + 1][curr_and & arr[i]]
The above recurrence relation can be defined as the number of subsets of sub-array arr[i…N-1] such that ANDing them with curr_and will yield the required AND value.
The recurrence relation is justified as there are only paths. Either, take the current element and AND it with curr_and or ignore it and move forward.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define maxN 20
#define maxM 64
int dp1[maxN][maxM];
bool v1[maxN][maxM];
int findCnt( int * arr, int i, int curr, int n, int m)
{
if (i == n) {
return (curr == m);
}
if (v1[i][curr])
return dp1[i][curr];
v1[i][curr] = 1;
dp1[i][curr]
= findCnt(arr, i + 1, curr, n, m)
+ findCnt(arr, i + 1, (curr & arr[i]), n, m);
return dp1[i][curr];
}
int main()
{
int arr[] = { 0, 0, 0 };
int n = sizeof (arr) / sizeof ( int );
int m = 0;
cout << findCnt(arr, 0, ((1 << 6) - 1), n, m);
return 0;
}
|
Java
class GFG
{
static int maxN = 20 ;
static int maxM = 64 ;
static int [][]dp1 = new int [maxN][maxM];
static boolean [][]v1 = new boolean [maxN][maxM];
static int findCnt( int []arr, int i,
int curr, int n, int m)
{
if (i == n)
{
return (curr == m ? 1 : 0 );
}
if (v1[i][curr])
return dp1[i][curr];
v1[i][curr] = true ;
dp1[i][curr] = findCnt(arr, i + 1 , curr, n, m) +
findCnt(arr, i + 1 , (curr & arr[i]), n, m);
return dp1[i][curr];
}
public static void main(String []args)
{
int arr[] = { 0 , 0 , 0 };
int n = arr.length;
int m = 0 ;
System.out.println(findCnt(arr, 0 , (( 1 << 6 ) - 1 ), n, m));
}
}
|
Python3
import numpy as np
maxN = 20
maxM = 64
dp1 = np.zeros((maxN, maxM));
v1 = np.zeros((maxN, maxM));
def findCnt(arr, i, curr, n, m) :
if (i = = n) :
return (curr = = m);
if (v1[i][curr]) :
return dp1[i][curr];
v1[i][curr] = 1 ;
dp1[i][curr] = findCnt(arr, i + 1 , curr, n, m) + \
findCnt(arr, i + 1 , (curr & arr[i]), n, m);
return dp1[i][curr];
if __name__ = = "__main__" :
arr = [ 0 , 0 , 0 ];
n = len (arr);
m = 0 ;
print (findCnt(arr, 0 , (( 1 << 6 ) - 1 ), n, m));
|
C#
using System;
class GFG
{
static int maxN = 20;
static int maxM = 64;
static int [,]dp1 = new int [maxN, maxM];
static bool [,]v1 = new bool [maxN, maxM];
static int findCnt( int []arr, int i,
int curr, int n, int m)
{
if (i == n)
{
return (curr == m ? 1 : 0);
}
if (v1[i, curr])
return dp1[i, curr];
v1[i, curr] = true ;
dp1[i, curr] = findCnt(arr, i + 1, curr, n, m) +
findCnt(arr, i + 1, (curr & arr[i]), n, m);
return dp1[i, curr];
}
public static void Main(String []args)
{
int []arr = { 0, 0, 0 };
int n = arr.Length;
int m = 0;
Console.WriteLine(findCnt(arr, 0, ((1 << 6) - 1), n, m));
}
}
|
Javascript
<script>
var maxN = 20;
var maxM = 64;
var dp1 = Array.from(Array(maxN), ()=> Array(maxM));
var v1 = Array.from(Array(maxN), ()=> Array(maxM));
function findCnt(arr, i, curr, n, m)
{
if (i == n) {
return (curr == m);
}
if (v1[i][curr])
return dp1[i][curr];
v1[i][curr] = 1;
dp1[i][curr]
= findCnt(arr, i + 1, curr, n, m)
+ findCnt(arr, i + 1, (curr & arr[i]), n, m);
return dp1[i][curr];
}
var arr = [0, 0, 0];
var n = arr.length;
var m = 0;
document.write( findCnt(arr, 0, ((1 << 6) - 1), n, m));
</script>
|
Last Updated :
14 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...