Count of Subsets of a given Set with element X present in it
Last Updated :
27 Dec, 2022
Given an array arr[] of N unique positive integers and an element X, the task is to count the total possible number of subsets of the given set in which element X is present.
Examples:
Input: arr[] = [4, 5, 6, 7], X = 5
Output: 8
Explanation:
All subsets in which element 5 is present are:
{5}, {4, 5}, {5, 6}, {5, 7}, {4, 5, 6}, {4, 5, 7}, {5, 6, 7}, {4, 5, 6, 7}
Input: arr[] = [1, 2, 3], X = 1
Output: 4
Explanation:
All subsets in which element 1 is present are:
{1}, {1, 2}, {1, 3}, {1, 2, 3}
Naive Approach:
The simple solution is to generate all possible subsets of a given set which are 2^n, where n is the size of the given set, and count the number of subsets in which element X is present.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int CountSubSet( int arr[], int n, int X)
{
int N = pow (2, n);
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < n; j++) {
if (i & (1 << j))
if (arr[j] == X)
count += 1;
}
}
return count;
}
int main()
{
int arr[] = { 4, 5, 6, 7 };
int X = 5;
int n = sizeof (arr) / sizeof (arr[0]);
cout << CountSubSet(arr, n, X);
return 0;
}
|
Java
class GFG
{
static int CountSubSet( int arr[], int n, int X)
{
int N = ( int ) Math.pow( 2 , n);
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < n; j++)
{
if ((i & ( 1 << j)) != 0 )
if (arr[j] == X)
count += 1 ;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 6 , 7 };
int X = 5 ;
int n = arr.length;
System.out.print(CountSubSet(arr, n, X));
}
}
|
Python3
def CountSubSet(arr, n, X) :
N = 2 * * n;
count = 0 ;
for i in range (N) :
for j in range (n) :
if (i & ( 1 << j)) :
if (arr[j] = = X) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
arr = [ 4 , 5 , 6 , 7 ];
X = 5 ;
n = len (arr);
print (CountSubSet(arr, n, X));
|
C#
using System;
class GFG
{
static int CountSubSet( int []arr, int n, int X)
{
int N = ( int ) Math.Pow(2, n);
int count = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < n; j++)
{
if ((i & (1 << j)) != 0)
if (arr[j] == X)
count += 1;
}
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 6, 7 };
int X = 5;
int n = arr.Length;
Console.Write(CountSubSet(arr, n, X));
}
}
|
Javascript
<script>
function CountSubSet(arr, n, X) {
let N = Math.pow(2, n);
let count = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < n; j++) {
if (i & (1 << j))
if (arr[j] == X)
count += 1;
}
}
return count;
}
let arr = [4, 5, 6, 7];
let X = 5;
let n = arr.length;
document.write(CountSubSet(arr, n, X));
</script>
|
Time complexity: O(n * 2^n).
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient solution:
- An efficient solution is to use the fact that every element of the set is present in exactly 2^(n-1) subsets.
- Here, in this solution, first, check whether the given value X is present in a given set of elements or not.
- If X is present, then compute and return 2^(n-1), (using modular exponentiation to compute power 2^n-1).
- Otherwise, return 0.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int calculatePower( int b, int e)
{
int ans = 1;
while (e > 0) {
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
int CountSubSet( int arr[], int n, int X)
{
int count = 0, checkX = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == X) {
checkX = 1;
break ;
}
}
if (checkX == 1)
count = calculatePower(2, n - 1);
else
count = 0;
return count;
}
int main()
{
int arr[] = { 4, 5, 6, 7 };
int X = 5;
int n = sizeof (arr) / sizeof (arr[0]);
cout << CountSubSet(arr, n, X);
return 0;
}
|
Java
class GFG
{
static int calculatePower( int b, int e)
{
int ans = 1 ;
while (e > 0 )
{
if (e % 2 == 1 )
ans = ans * b;
e = e / 2 ;
b = b * b;
}
return ans;
}
static int CountSubSet( int arr[], int n, int X)
{
int count = 0 , checkX = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1 ;
break ;
}
}
if (checkX == 1 )
count = calculatePower( 2 , n - 1 );
else
count = 0 ;
return count;
}
public static void main (String[] args)
{
int arr[] = { 4 , 5 , 6 , 7 };
int X = 5 ;
int n = arr.length;
System.out.println(CountSubSet(arr, n, X));
}
}
|
Python3
def calculatePower(b, e) :
ans = 1 ;
while (e > 0 ) :
if (e % 2 = = 1 ) :
ans = ans * b;
e = e / / 2 ;
b = b * b;
return ans;
def CountSubSet(arr, n, X) :
count = 0 ; checkX = 0 ;
for i in range (n) :
if (arr[i] = = X) :
checkX = 1 ;
break ;
if (checkX = = 1 ) :
count = calculatePower( 2 , n - 1 );
else :
count = 0 ;
return count;
if __name__ = = "__main__" :
arr = [ 4 , 5 , 6 , 7 ];
X = 5 ;
n = len (arr);
print (CountSubSet(arr, n, X));
|
C#
using System;
class GFG
{
static int calculatePower( int b, int e)
{
int ans = 1;
while (e > 0)
{
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
static int CountSubSet( int []arr, int n, int X)
{
int count = 0, checkX = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1;
break ;
}
}
if (checkX == 1)
count = calculatePower(2, n - 1);
else
count = 0;
return count;
}
public static void Main()
{
int []arr = { 4, 5, 6, 7 };
int X = 5;
int n = arr.Length;
Console.WriteLine(CountSubSet(arr, n, X));
}
}
|
Javascript
<script>
function calculatePower(b, e)
{
let ans = 1;
while (e > 0) {
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
function CountSubSet(arr, n, X)
{
let count = 0, checkX = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == X) {
checkX = 1;
break ;
}
}
if (checkX == 1)
count = calculatePower(2, n - 1);
else
count = 0;
return count;
}
let arr = [4, 5, 6, 7];
let X = 5;
let n = arr.length;
document.write(CountSubSet(arr, n, X));
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...