📌  相关文章
📜  给定大小的所有子集的总和 (=K)

📅  最后修改于: 2022-05-13 01:56:08.263000             🧑  作者: Mango

给定大小的所有子集的总和 (=K)

给定一个由N个整数和一个正整数K组成的数组arr[] ,任务是找到所有大小为K的子集的总和。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子集,并找到大小为K的那些子集的元素之和。在找到所有K 大小的子集的总和后,打印作为结果获得的所有总和的总和。

时间复杂度: O(K*2 N )
辅助空间: O(1)

有效方法:上述方法也可以通过观察求和序列中每个元素arr[i]的出现次数取决于 N 和 K 的值这一事实来优化。

让我们在求和序列中找到一般元素 x 的出现:

  • x 在所有大小的求和序列中的出现 = k 个子集 = n-1 C k-1

因此, arr[] 的每个元素的频率在求和方程 = n-1 C k-1中是相同的。因此,所有子集的总和 =(数组所有元素的总和)* n-1 C k-1

请按照以下步骤解决给定的问题:

  • 初始化变量,比如freq为 0 并计算n-1 C k-1
  • 初始化变量,例如sum0以存储所有数组元素的总和。
  • 执行上述步骤后,打印sum*freq的值作为结果和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum of all
// sub-sets of size K
void findSumOfAllSubsets(int arr[], int n, int k)
{
 
    // Frequency of each array element
    // in summation equation.
    int factorial_N=1, factorial_d=1, factorial_D=1;
   
    //calculate factorial of n-1
    for(int i=1; i<=n-1; i++)
      factorial_N*=i;
    //calculate factorial of k-1
    for(int i=1; i<=k-1; i++)
      factorial_d*=i;
    //calculate factorial of n-k
    for(int i=1; i<=n-k; i++)
      factorial_D*=i;
   
    int freq = factorial_N/(factorial_d * factorial_D);
 
    // Calculate sum of array.
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
     
    // Sum of all subsets of size k.
    sum = sum * freq;
 
    cout <<"Sum of all subsets of size = "< "<< sum << endl;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 4, 5 };
    int n = 4, k = 2;
 
    findSumOfAllSubsets(arr, n, k);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
 
class GFG
{
   
    // Function to find the sum of all
    // sub-sets of size K
    static void findSumOfAllSubsets(int[] arr, int n, int k)
    {
 
        // Frequency of each array element
        // in summation equation.
        int factorial_N = 1, factorial_d = 1,
            factorial_D = 1;
 
        // calculate factorial of n-1
        for (int i = 1; i <= n - 1; i++)
            factorial_N *= i;
       
        // calculate factorial of k-1
        for (int i = 1; i <= k - 1; i++)
            factorial_d *= i;
       
        // calculate factorial of n-k
        for (int i = 1; i <= n - k; i++)
            factorial_D *= i;
 
        int freq
            = factorial_N / (factorial_d * factorial_D);
 
        // Calculate sum of array.
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
 
        // Sum of all subsets of size k.
        sum = sum * freq;
 
        System.out.println("Sum of all subsets of size = "
                           + k + " is => " + sum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] arr = { 1, 2, 4, 5 };
        int n = 4, k = 2;
 
        findSumOfAllSubsets(arr, n, k);
    }
}
 
// This code is contributed by maddler.


Python3
# Python 3 program for the above approach
 
# Function to find the sum of all
# sub-sets of size K
def findSumOfAllSubsets(arr, n, k):
   
    # Frequency of each array element
    # in summation equation.
    factorial_N = 1
    factorial_d = 1
    factorial_D = 1
  
    # calculate factorial of n-1
    for i in range(1, n, 1):
        factorial_N *= i
         
    # calculate factorial of k-1
    for i in range(1, k, 1):
        factorial_d *= i
         
    # calculate factorial of n-k
    for i in range(1, n - k + 1, 1):
        factorial_D *= i
  
    freq = factorial_N//(factorial_d * factorial_D)
 
    # Calculate sum of array.
    sum = 0
    for i in range(n):
        sum += arr[i]
 
    # Sum of all subsets of size k.
    sum = sum * freq
    print("Sum of all subsets of size = ",k," is =>",sum)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 4, 5]
    n = 4
    k = 2
 
    findSumOfAllSubsets(arr, n, k)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to find the sum of all
    // sub-sets of size K
    static void findSumOfAllSubsets(int[] arr, int n, int k)
    {
 
        // Frequency of each array element
        // in summation equation.
        int factorial_N = 1, factorial_d = 1,
            factorial_D = 1;
 
        // calculate factorial of n-1
        for (int i = 1; i <= n - 1; i++)
            factorial_N *= i;
        // calculate factorial of k-1
        for (int i = 1; i <= k - 1; i++)
            factorial_d *= i;
        // calculate factorial of n-k
        for (int i = 1; i <= n - k; i++)
            factorial_D *= i;
 
        int freq
            = factorial_N / (factorial_d * factorial_D);
 
        // Calculate sum of array.
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
 
        // Sum of all subsets of size k.
        sum = sum * freq;
 
        Console.WriteLine("Sum of all subsets of size = "
                          + k + " is => " + sum);
    }
 
    // Driver Code
    public static void Main()
    {
 
        int[] arr = { 1, 2, 4, 5 };
        int n = 4, k = 2;
 
        findSumOfAllSubsets(arr, n, k);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
36

时间复杂度: O(N)
辅助空间: O(1)