Open In App

Number of subsets with a given OR value

Last Updated : 10 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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:

 

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define maxN 20
#define maxM 64
 
// To store states of DP
int dp[maxN][maxM];
bool v[maxN][maxM];
 
// Function to return the required count
int findCnt(int* arr, int i, int curr, int n, int m)
{
    // Base case
    if (i == n) {
        return (curr == m);
    }
 
    // If the state has been solved before
    // return the value of the state
    if (v[i][curr])
        return dp[i][curr];
 
    // Setting the state as solved
    v[i][curr] = 1;
 
    // Recurrence relation
    dp[i][curr]
        = findCnt(arr, i + 1, curr, n, m)
          + findCnt(arr, i + 1, (curr | arr[i]), n, m);
 
    return dp[i][curr];
}
 
// Driver code
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




// Java implementation of the approach
import java.util.*;
class GFG
{
     
static int maxN = 20;
static int maxM = 64;
 
// To store states of DP
static int [][]dp = new int[maxN][maxM];
static boolean [][]v = new boolean[maxN][maxM];
 
// Function to return the required count
static int findCnt(int[] arr, int i,
                   int curr, int n, int m)
{
    // Base case
    if (i == n)
    {
        return (curr == m ? 1 : 0);
    }
 
    // If the state has been solved before
    // return the value of the state
    if (v[i][curr])
        return dp[i][curr];
 
    // Setting the state as solved
    v[i][curr] = true;
 
    // Recurrence relation
    dp[i][curr] = findCnt(arr, i + 1, curr, n, m) +
                  findCnt(arr, i + 1, (curr | arr[i]), n, m);
 
    return dp[i][curr];
}
 
// Driver code
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));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
import numpy as np
 
maxN = 20
maxM = 64
 
# To store states of DP
dp = np.zeros((maxN, maxM));
v = np.zeros((maxN, maxM));
 
# Function to return the required count
def findCnt(arr, i, curr, n, m) :
 
    # Base case
    if (i == n) :
        return (curr == m);
 
    # If the state has been solved before
    # return the value of the state
    if (v[i][curr]) :
        return dp[i][curr];
 
    # Setting the state as solved
    v[i][curr] = 1;
 
    # Recurrence relation
    dp[i][curr] = findCnt(arr, i + 1, curr, n, m) + \
                  findCnt(arr, i + 1, (curr | arr[i]), n, m);
 
    return dp[i][curr];
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 3, 2 ];
    n = len(arr);
    m = 3;
 
    print(findCnt(arr, 0, 0, n, m));
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
     
class GFG
{
     
static int maxN = 20;
static int maxM = 64;
 
// To store states of DP
static int [,]dp = new int[maxN, maxM];
static Boolean [,]v = new Boolean[maxN, maxM];
 
// Function to return the required count
static int findCnt(int[] arr, int i,
                   int curr, int n, int m)
{
    // Base case
    if (i == n)
    {
        return (curr == m ? 1 : 0);
    }
 
    // If the state has been solved before
    // return the value of the state
    if (v[i, curr])
        return dp[i, curr];
 
    // Setting the state as solved
    v[i, curr] = true;
 
    // Recurrence relation
    dp[i, curr] = findCnt(arr, i + 1, curr, n, m) +
                  findCnt(arr, i + 1, (curr | arr[i]), n, m);
 
    return dp[i, curr];
}
 
// Driver code
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));
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript implementation of the approach
var maxN = 20
var maxM = 64
 
// To store states of DP
var dp = Array.from(Array(maxN), ()=> Array(maxM));
var v = Array.from(Array(maxN), ()=> Array(maxM));
 
// Function to return the required count
function findCnt(arr, i, curr, n, m)
{
    // Base case
    if (i == n) {
        return (curr == m);
    }
 
    // If the state has been solved before
    // return the value of the state
    if (v[i][curr])
        return dp[i][curr];
 
    // Setting the state as solved
    v[i][curr] = 1;
 
    // Recurrence relation
    dp[i][curr]
        = findCnt(arr, i + 1, curr, n, m)
          + findCnt(arr, i + 1, (curr | arr[i]), n, m);
 
    return dp[i][curr];
}
 
// Driver code
var arr = [2, 3, 2 ];
var n = arr.length;
var m = 3;
document.write( findCnt(arr, 0, 0, n, m));
 
</script>   


Output

4

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.



Similar Reads

Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once
Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once. Examples: Input: arr[] = { 3, 4, 2, 1, 5 } Output: 3 Explanation: Possible pairs are { (1, 2), (1, 3), (1, 4), (1,
6 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2, 9, 5}, K = 10Output: 2Explanation:All such disjoi
8 min read
Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
Given an array of size N. The task is to partition the given array into two subsets such that the average of all the elements in both subsets is equal. If no such partition exists print -1. Otherwise, print the partitions. If multiple solutions exist, print the solution where the length of the first subset is minimum. If there is still a tie then p
14 min read
Sum of subsets of all the subsets of an array | O(3^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Sum of subsets of all the subsets of an array | O(2^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Divide array in two Subsets such that sum of square of sum of both subsets is maximum
Given an integer array arr[], the task is to divide this array into two non-empty subsets such that the sum of the square of the sum of both the subsets is maximum and sizes of both the subsets must not differ by more than 1.Examples: Input: arr[] = {1, 2, 3} Output: 26 Explanation: Sum of Subset Pairs are as follows (1)2 + (2 + 3)2 = 26 (2)2 + (1
6 min read
Sum of subsets of all the subsets of an array | O(N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
7 min read
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 &amp; 3 = 2 {3, 2} = 3 &amp; 2 = 2 {2, 2} = 2 &amp; 2 = 2 {2, 3, 2} = 2 &amp; 3 &amp; 2 = 2Input: arr[] =
6 min read
Count of Subsets containing only the given value K
Given an array arr[] and a number K which is present in the array at least once, the task is to find the number of subsets in the array such that each subset contains only the given value K. Examples: Input: arr[] = {1, 0, 0, 1, 0, 1, 2, 5, 2, 1}, K = 0 Output: 4 Explanation: From the two 0's present in the array at the index 2 and 3, 3 subsequence
7 min read
Find maximum AND value among all K-size subsets of given Array
Given an array arr[] containing N non-negative integers, the task is to find the maximum AND value among all the subsets having length K. Examples: Input: arr[] = {1, 6, 9, 7}, K = 1Output: 9Explanation: As only one element is allowed 9 is the greatest value that can be obtained. Input: arr[] = {3, 3, 3}, K = 2Output: 3 Input: arr[] = {7, 8, 9, 10,
7 min read