给定一个由N个唯一正整数组成的数组arr []和一个元素X ,任务是计算存在元素X的给定集合的子集的总数。
例子:
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}
天真的方法:
一种简单的解决方案是生成给定集合的所有可能子集,它们均为2 ^ n,其中n是给定集合的大小,并对元素X存在的子集的数量进行计数。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
int CountSubSet(int arr[], int n, int X)
{
// N stores total number of subsets
int N = pow(2, n);
int count = 0;
// Generate each subset one by one
for (int i = 0; i < N; i++) {
// Check every bit of i
for (int j = 0; j < n; j++) {
// if j'th bit of i is set,
// check arr[j] with X
if (i & (1 << j))
if (arr[j] == X)
count += 1;
}
}
return count;
}
// Driver code
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
// Java code to implement the above approach
class GFG
{
static int CountSubSet(int arr[], int n, int X)
{
// N stores total number of subsets
int N = (int) Math.pow(2, n);
int count = 0;
// Generate each subset one by one
for (int i = 0; i < N; i++)
{
// Check every bit of i
for (int j = 0; j < n; j++)
{
// if j'th bit of i is set,
// check arr[j] with X
if ((i & (1 << j)) != 0)
if (arr[j] == X)
count += 1;
}
}
return count;
}
// Driver code
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));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 code to implement the above approach
def CountSubSet(arr, n, X) :
# N stores total number of subsets
N = 2 ** n;
count = 0;
# Generate each subset one by one
for i in range(N) :
# Check every bit of i
for j in range(n) :
# if j'th bit of i is set,
# check arr[j] with X
if (i & (1 << j)) :
if (arr[j] == X) :
count += 1;
return count;
# Driver code
if __name__ == "__main__" :
arr = [ 4, 5, 6, 7 ];
X = 5;
n = len(arr);
print(CountSubSet(arr, n, X));
# This code is contributed by AnkitRai01
C#
// C# code to implement the above approach
using System;
class GFG
{
static int CountSubSet(int []arr, int n, int X)
{
// N stores total number of subsets
int N = (int) Math.Pow(2, n);
int count = 0;
// Generate each subset one by one
for (int i = 0; i < N; i++)
{
// Check every bit of i
for (int j = 0; j < n; j++)
{
// if j'th bit of i is set,
// check arr[j] with X
if ((i & (1 << j)) != 0)
if (arr[j] == X)
count += 1;
}
}
return count;
}
// Driver code
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));
}
}
// This code is contributed by 29AjayKumar
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to calculate (2^(n-1))
int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0) {
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
int CountSubSet(int arr[], int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++) {
if (arr[i] == X) {
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver Function
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
// Java implementation of above approach
class GFG
{
// Function to calculate (2^(n-1))
static int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0)
{
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
static int CountSubSet(int arr[], int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver code
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));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of above approach
# Function to calculate (2^(n-1))
def calculatePower(b, e) :
# Initially initialize answer to 1
ans = 1;
while (e > 0) :
# If e is odd,
# multiply b with answer
if (e % 2 == 1) :
ans = ans * b;
e = e // 2;
b = b * b;
return ans;
# Function to count subsets in which
# X element is present
def CountSubSet(arr, n, X) :
count = 0; checkX = 0;
# Check if X is present in
# given subset or not
for i in range(n) :
if (arr[i] == X) :
checkX = 1;
break;
# If X is present in set
# then calculate 2^(n-1) as count
if (checkX == 1) :
count = calculatePower(2, n - 1);
# if X is not present in a given set
else :
count = 0;
return count;
# Driver code
if __name__ == "__main__" :
arr = [ 4, 5, 6, 7 ];
X = 5;
n = len(arr);
print(CountSubSet(arr, n, X));
# This code is contributed by AnkitRai01
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to calculate (2^(n-1))
static int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0)
{
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
static int CountSubSet(int []arr, int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver code
public static void Main()
{
int []arr = { 4, 5, 6, 7 };
int X = 5;
int n = arr.Length;
Console.WriteLine(CountSubSet(arr, n, X));
}
}
// This code is contributed by AnkitRai01
输出:
8
时间复杂度: O(n * 2 ^ n)。
高效的解决方案:
- 一个有效的解决方案是使用集合中的每个元素都精确地存在于2 ^(n-1)个子集中的事实。
- 在此,在此解决方案中,首先检查给定值X是否存在于给定的一组元素中。
- 如果存在X,则计算并返回2 ^(n-1)(使用模幂计算2 ^ n-1的幂)。
- 否则,返回0。
下面是上述方法的实现
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to calculate (2^(n-1))
int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0) {
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
int CountSubSet(int arr[], int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++) {
if (arr[i] == X) {
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver Function
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
// Java implementation of above approach
class GFG
{
// Function to calculate (2^(n-1))
static int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0)
{
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
static int CountSubSet(int arr[], int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver code
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));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of above approach
# Function to calculate (2^(n-1))
def calculatePower(b, e) :
# Initially initialize answer to 1
ans = 1;
while (e > 0) :
# If e is odd,
# multiply b with answer
if (e % 2 == 1) :
ans = ans * b;
e = e // 2;
b = b * b;
return ans;
# Function to count subsets in which
# X element is present
def CountSubSet(arr, n, X) :
count = 0; checkX = 0;
# Check if X is present in
# given subset or not
for i in range(n) :
if (arr[i] == X) :
checkX = 1;
break;
# If X is present in set
# then calculate 2^(n-1) as count
if (checkX == 1) :
count = calculatePower(2, n - 1);
# if X is not present in a given set
else :
count = 0;
return count;
# Driver code
if __name__ == "__main__" :
arr = [ 4, 5, 6, 7 ];
X = 5;
n = len(arr);
print(CountSubSet(arr, n, X));
# This code is contributed by AnkitRai01
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to calculate (2^(n-1))
static int calculatePower(int b, int e)
{
// Initially initialize answer to 1
int ans = 1;
while (e > 0)
{
// If e is odd,
// multiply b with answer
if (e % 2 == 1)
ans = ans * b;
e = e / 2;
b = b * b;
}
return ans;
}
// Function to count subsets in which
// X element is present
static int CountSubSet(int []arr, int n, int X)
{
int count = 0, checkX = 0;
// Check if X is present in
// given subset or not
for (int i = 0; i < n; i++)
{
if (arr[i] == X)
{
checkX = 1;
break;
}
}
// If X is present in set
// then calculate 2^(n-1) as count
if (checkX == 1)
count = calculatePower(2, n - 1);
// if X is not present in a given set
else
count = 0;
return count;
}
// Driver code
public static void Main()
{
int []arr = { 4, 5, 6, 7 };
int X = 5;
int n = arr.Length;
Console.WriteLine(CountSubSet(arr, n, X));
}
}
// This code is contributed by AnkitRai01
输出:
8
时间复杂度: O(n)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。