📜  计算具有不同偶数的子集

📅  最后修改于: 2021-10-27 08:54:45             🧑  作者: Mango

给定一个 n 个数字的序列。任务是计算给定集合的所有子集,这些子集只有偶数并且都是不同的。

注意:根据集合的性质,如果两个子集具有相同的元素集合,则将它们视为一个。例如: [2, 4, 8] 和 [4, 2, 8] 被认为是相同的。

例子:

Input : {4, 2, 1, 9, 2, 6, 5, 3} 
Output : 7
The subsets are:
[4], [2], [6], [4, 2], 
[2, 6], [4, 6], [4, 2, 6]

Input : {10, 3, 4, 2, 4, 20, 10, 6, 8, 14, 2, 6, 9}
Output : 127

一个简单的方法是考虑所有子集并检查它们是否满足给定条件。时间复杂度将呈指数级增长。
一种有效的方法是计算不同偶数的数量。让这成为ceven 。然后应用公式:
2西文– 1

这类似于计算给定的 n 个元素集合的子集数量。因为不考虑空集,所以减去1

C++
// C++ implementation to count subsets having
// even numbers only and all are distinct
#include 
using namespace std;
 
// function to count the
// required subsets
int countSubsets(int arr[], int n)
{
    unordered_set us;
    int even_count = 0;
         
    // inserting even numbers in the set 'us'
    // single copy of each number is retained
    for (int i=0; i:: iterator itr;
     
    // distinct even numbers
    even_count = us.size();
     
    // total count of required subsets
    return (pow(2, even_count) - 1);
}
 
// Driver program to test above
int main()
{
    int arr[] = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Number of subsets = "
         << countSubsets(arr, n);
    return 0;    
}


Java
// Java implementation to count subsets having
// even numbers only and all are distinct
import java.util.*;
 
class GFG
{
 
// function to count the
// required subsets
static int countSubsets(int arr[], int n)
{
    HashSet us = new HashSet<>();
    int even_count = 0;
         
    // inserting even numbers in the set 'us'
    // single copy of each number is retained
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 0)
            us.add(arr[i]);
     
     
    // counting distinct even numbers
    even_count=us.size();
     
    // total count of required subsets
    return (int) (Math.pow(2, even_count) - 1);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = arr.length;
    System.out.println("Number of subsets = "
        + countSubsets(arr, n));
}
}
 
// This code contributed by Rajput-Ji


Python3
# python implementation to count subsets having
# even numbers only and all are distinct
 
#function to count the required subsets
def countSubSets(arr, n):
    us = set()
    even_count = 0
 
    # inserting even numbers in the set 'us'
    # single copy of each number is retained
    for i in range(n):
        if arr[i] % 2 == 0:
            us.add(arr[i])
 
    # counting distinct even numbers
    even_count = len(us)
 
    # total count of required subsets
    return pow(2, even_count)-  1
 
 
# Driver program
arr = [4, 2, 1, 9, 2, 6, 5, 3]
n = len(arr)
print("Numbers of subset=", countSubSets(arr,n))
 
# This code is contributed by Shrikant13


C#
// C# implementation to count subsets having
// even numbers only and all are distinct
using System;
using System.Collections.Generic;
 
class GFG
{
 
// function to count the
// required subsets
static int countSubsets(int []arr, int n)
{
    HashSet us = new HashSet();
    int even_count = 0;
         
    // inserting even numbers in the set 'us'
    // single copy of each number is retained
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 0)
            us.Add(arr[i]);
     
     
    // counting distinct even numbers
    even_count = us.Count;
     
    // total count of required subsets
    return (int) (Math.Pow(2, even_count) - 1);
}
 
// Driver code
public static void Main(String[] args)
{
    int[] arr = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = arr.Length;
    Console.WriteLine("Number of subsets = "
        + countSubsets(arr, n));
}
}
 
// This code contributed by Rajput-Ji


Javascript


输出:

Number of subsets = 7

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程