📜  长度为 K 的所有子序列的总和

📅  最后修改于: 2021-09-04 08:19:13             🧑  作者: Mango

给定一个数组 arr[]和一个整数 K ,任务是从给定的数组中找到所有 K 个长度子序列的总和。

例子:

方法:
为了解决上面提到的问题,我们必须考虑所有K长度的子序列,即“n 选择 k”,即k * nCk

  • 所有 K 个长度的子序列中的总元素数为k * nCk ,每个元素出现的可能性都是一样的。
  • 所以每个元素出现((k * nCk) / n )时间和它的贡献arr[i] * ( (k*nCk)/n )结果中。
  • 因此,所有 K 个长度的子序列之和为sum(array) * ( (k * nCk) / n )

下面是上述方法的实现:

C++
// C++ implementation to find sum
// of all subsequences of length K
  
#include 
using namespace std;
  
int fact(int n);
  
// Function to find nCr
int nCr(int n, int r)
{
    return fact(n)
           / (fact(r)
              * fact(n - r));
}
  
// Function that returns
// factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function for finding sum
// of all K length subsequences
int sumSubsequences(
    int arr[], int n, int k)
{
  
    int sum = 0;
  
    // Calculate the sum of array
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    int kLengthSubSequence;
  
    // Calculate nCk
    kLengthSubSequence = nCr(n, k);
  
    int ans
        = sum
          * ((k * kLengthSubSequence)
             / n);
  
    // Return the final result
    return ans;
}
  
// Driver code
int main()
{
  
    int arr[] = { 7, 8, 9, 2 };
  
    int K = 2;
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << sumSubsequences(arr, n, K);
    return 0;
}


Java
// Java implementation to find sum
// of all subsequences of length K
class GFG{
  
// Function to find nCr
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
  
// Function that returns
// factorial of n
static int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function for finding sum
// of all K length subsequences
static int sumSubsequences(int arr[], 
                           int n, int k)
{
    int sum = 0;
  
    // Calculate the sum of array
    for (int i = 0; i < n; i++) 
    {
        sum += arr[i];
    }
    int kLengthSubSequence;
  
    // Calculate nCk
    kLengthSubSequence = nCr(n, k);
  
    int ans = sum * ((k * kLengthSubSequence) / n);
  
    // Return the final result
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 7, 8, 9, 2 };
  
    int K = 2;
  
    int n = arr.length;
  
    System.out.print(sumSubsequences(arr, n, K));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation to find sum 
# of all subsequences of length K
  
# Function to find nCr 
def nCr(n, r):
      
    return fact(n) / (fact(r) * 
                      fact(n - r))
  
# Function that returns 
# factorial of n
def fact(n):
      
    res = 1
    for i in range(2, n + 1):
        res = res * i 
    return res
      
# Function for finding sum 
# of all K length subsequences
def sumSubsequences(arr, n, k):
      
    sum = 0
      
    # Calculate the sum of array 
    for i in range(0, n):
        sum = sum + arr[i]
      
    # Calculate nCk     
    kLengthSubSequence = nCr(n, k)
    ans = sum * ((k * kLengthSubSequence) / n);
      
    # Return the final result 
    return ans
  
# Driver Code 
arr = [ 7, 8, 9, 2 ]
k = 2
n = len(arr)
  
print(sumSubsequences(arr, n, k))
  
# This code is contributed by skylags


C#
// C# implementation to find sum
// of all subsequences of length K
using System;
  
class GFG{
      
// Function to find nCr
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
      
// Function that returns
// factorial of n
static int fact(int n)
{
    int res = 1;
      
    for(int i = 2; i <= n; i++)
       res = res * i;
    return res;
}
      
// Function for finding sum
// of all K length subsequences
static int sumSubsequences(int[] arr, 
                           int n, int k)
{
    int sum = 0;
      
    // Calculate the sum of array
    for(int i = 0; i < n; i++) 
    {
       sum += arr[i];
    }
      
    int kLengthSubSequence;
      
    // Calculate nCk
    kLengthSubSequence = nCr(n, k);
    int ans = sum * ((k * kLengthSubSequence) / n);
      
    // Return the final result
    return ans;
}
  
// Driver code
static void Main() 
{
    int[] arr = { 7, 8, 9, 2 };
    int K = 2;
    int n = arr.Length;
          
    Console.Write(sumSubsequences(arr, n, K));
}
}
  
// This code is contributed by divyeshrabadiya07


Javascript


输出:
78

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live