Number of subsets with a given OR value
Given an array arr[] of length N, the task is to find the number of subsets with a given OR value M.
Examples:
Input: arr[] = {2, 3, 2} M = 3
Output: 4
All possible subsets and there OR values are:
{2} = 2
{3} = 3
{2} = 2
{2, 3} = 2 | 3 = 3
{3, 2} = 3 | 2 = 3
{2, 2} = 2 | 2 = 2
{2, 3, 2} = 2 | 3 | 2 = 3
Input: arr[] = {1, 3, 2, 2}, M = 5
Output: 0
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 OR 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_or] = dp[i + 1][curr_or] + dp[i + 1][curr_or | arr[i]]
The above recurrence relation can be defined as the number of subsets of sub-array arr[i…N-1] such that ORing them with curr_or will yield the required OR value.
The recurrence relation is justified as there are only paths. Either, take the current element and OR it with curr_or 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 dp[maxN][maxM];
bool v[maxN][maxM];
int findCnt( int * arr, int i, int curr, int n, int m)
{
if (i == n) {
return (curr == m);
}
if (v[i][curr])
return dp[i][curr];
v[i][curr] = 1;
dp[i][curr]
= findCnt(arr, i + 1, curr, n, m)
+ findCnt(arr, i + 1, (curr | arr[i]), n, m);
return dp[i][curr];
}
int main()
{
int arr[] = { 2, 3, 2 };
int n = sizeof (arr) / sizeof ( int );
int m = 3;
cout << findCnt(arr, 0, 0, n, m);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxN = 20 ;
static int maxM = 64 ;
static int [][]dp = new int [maxN][maxM];
static boolean [][]v = 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 (v[i][curr])
return dp[i][curr];
v[i][curr] = true ;
dp[i][curr] = findCnt(arr, i + 1 , curr, n, m) +
findCnt(arr, i + 1 , (curr | arr[i]), n, m);
return dp[i][curr];
}
public static void main(String []args)
{
int arr[] = { 2 , 3 , 2 };
int n = arr.length;
int m = 3 ;
System.out.println(findCnt(arr, 0 , 0 , n, m));
}
}
|
Python3
import numpy as np
maxN = 20
maxM = 64
dp = np.zeros((maxN, maxM));
v = np.zeros((maxN, maxM));
def findCnt(arr, i, curr, n, m) :
if (i = = n) :
return (curr = = m);
if (v[i][curr]) :
return dp[i][curr];
v[i][curr] = 1 ;
dp[i][curr] = findCnt(arr, i + 1 , curr, n, m) + \
findCnt(arr, i + 1 , (curr | arr[i]), n, m);
return dp[i][curr];
if __name__ = = "__main__" :
arr = [ 2 , 3 , 2 ];
n = len (arr);
m = 3 ;
print (findCnt(arr, 0 , 0 , n, m));
|
C#
using System;
class GFG
{
static int maxN = 20;
static int maxM = 64;
static int [,]dp = new int [maxN, maxM];
static Boolean [,]v = 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 (v[i, curr])
return dp[i, curr];
v[i, curr] = true ;
dp[i, curr] = findCnt(arr, i + 1, curr, n, m) +
findCnt(arr, i + 1, (curr | arr[i]), n, m);
return dp[i, curr];
}
public static void Main(String []args)
{
int []arr = { 2, 3, 2 };
int n = arr.Length;
int m = 3;
Console.WriteLine(findCnt(arr, 0, 0, n, m));
}
}
|
Javascript
<script>
var maxN = 20
var maxM = 64
var dp = Array.from(Array(maxN), ()=> Array(maxM));
var v = Array.from(Array(maxN), ()=> Array(maxM));
function findCnt(arr, i, curr, n, m)
{
if (i == n) {
return (curr == m);
}
if (v[i][curr])
return dp[i][curr];
v[i][curr] = 1;
dp[i][curr]
= findCnt(arr, i + 1, curr, n, m)
+ findCnt(arr, i + 1, (curr | arr[i]), n, m);
return dp[i][curr];
}
var arr = [2, 3, 2 ];
var n = arr.length;
var m = 3;
document.write( findCnt(arr, 0, 0, n, m));
</script>
|
Time Complexity: O(maxN*maxM), where maxN and maxM are the constant defined.
Auxiliary Space: O(maxN*maxM), where maxN and maxM are the constant defined.
Last Updated :
10 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...