📌  相关文章
📜  计算其元素平均值等于K的子序列

📅  最后修改于: 2021-04-18 02:27:10             🧑  作者: Mango

给定由N个整数和整数K组成的数组arr [] ,任务是计算给定数组的平均K子序列数。

例子:

天真的方法:解决问题的最简单方法是使用递归。请按照以下步骤解决问题:

  1. 每个数组元素可能有两个选择,即要么将当前元素包括在总和中,要么将当前元素排除在总和中,并为每个递归调用增加当前索引。
  2. 对于上述两种可能性,返回平均数为K的子序列数。
  3. 基本情况是检查是否已达到最后一个索引。基本情况下的平均值可以通过除以该子序列中包含的数组元素的总和来计算。
  4. 如果平均值等于K ,则返回1 。否则,返回0

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

高效方法:可以使用动态编程来优化上述方法。请按照以下步骤解决问题:

  • 初始化一个3D数组,例如dp [] [] [] ,其中dp [i] [k] [s]是从前i个整数中选择k个整数,以使所选整数之和为s的方式的数量
  • 遍历数组。
  • 每个元素有两个可能的选项,即包含或排除它。
  • 如果包括索引i处的当前元素,则下一个索引状态将为i +1,并且所选元素的数量将从k增加到k + 1 ,并且总和s将增加到s + arr [i]
  • 如果排除索引i处的当前元素,则只有索引i将增加到i + 1,而所有其他状态将保持不变。
  • 以下是此问题的dp过渡状态:
  • 最后,答案将是所有j ( 1≤j≤N) dp [N] [j] [K * j]的总和。

下面是上述方法的实现:

C++14
// C++ program for the above approach
 
#include 
using namespace std;
 
// Stores the dp states
int dp[101][101][1001];
 
// Function to find the count of
// subsequences having average K
int countAverage(int n, int K, int* arr)
{
 
    // Base condition
    dp[0][0][0] = 1;
 
    // Three loops for three states
    for (int i = 0; i < n; i++) {
        for (int k = 0; k < n; k++) {
            for (int s = 0; s <= 1000; s++) {
 
                // Recurrence relation
                dp[i + 1][k + 1][s + arr[i]]
                    += dp[i][k][s];
                dp[i + 1][k][s] += dp[i][k][s];
            }
        }
    }
 
    // Stores the sum of dp[n][j][K*j]
    // all possible values of j with
    // average K and sum K * j
    int cnt = 0;
 
    // Iterate over the range [1, N]
    for (int j = 1; j <= n; j++) {
        cnt += dp[n][j][K * j];
    }
 
    // Return the final count
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 9, 7, 8, 9 };
    int K = 8;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << countAverage(N, K, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Stores the dp states
static int [][][]dp = new int[101][101][1001];
 
// Function to find the count of
// subsequences having average K
static int countAverage(int n, int K, int[] arr)
{
 
    // Base condition
    dp[0][0][0] = 1;
 
    // Three loops for three states
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < n; k++)
        {
            for (int s = 0; s <= 100; s++)
            {
 
                // Recurrence relation
                dp[i + 1][k + 1][s + arr[i]]
                    += dp[i][k][s];
                dp[i + 1][k][s] += dp[i][k][s];
            }
        }
    }
 
    // Stores the sum of dp[n][j][K*j]
    // all possible values of j with
    // average K and sum K * j
    int cnt = 0;
 
    // Iterate over the range [1, N]
    for (int j = 1; j <= n; j++)
    {
        cnt += dp[n][j][K * j];
    }
 
    // Return the final count
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 9, 7, 8, 9 };
    int K = 8;
    int N = arr.length;
    System.out.print(countAverage(N, K, arr));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Stores the dp states
dp = [[[0 for i in range(1001)] for i in range(101)] for i in range(101)]
 
# Function to find the count of
# subsequences having average K
def countAverage(n, K, arr):
    global dp
    dp[0][0][0] = 1
 
    # Three loops for three states
    for i in range(n):
        for k in range(n):
            for s in range(100):
 
                # Recurrence relation
                dp[i + 1][k + 1][s + arr[i]] += dp[i][k][s]
                dp[i + 1][k][s] += dp[i][k][s]
 
    # Stores the sum of dp[n][j][K*j]
    # all possible values of j with
    # average K and sum K * j
    cnt = 0
 
    # Iterate over the range [1, N]
    for j in range(1, n + 1):
        cnt += dp[n][j][K * j]
 
    # Return the final count
    return cnt
 
# Driver Code
if __name__ == '__main__':
    arr= [9, 7, 8, 9]
    K = 8
    N = len(arr)
 
    print(countAverage(N, K, arr))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Stores the dp states
static int [,,]dp = new int[101, 101, 1001];
 
// Function to find the count of
// subsequences having average K
static int countAverage(int n, int K, int[] arr)
{
 
    // Base condition
    dp[0, 0, 0] = 1;
 
    // Three loops for three states
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < n; k++)
        {
            for (int s = 0; s <= 100; s++)
            {
 
                // Recurrence relation
                dp[i + 1, k + 1, s + arr[i]]
                    += dp[i, k, s];
                dp[i + 1, k, s] += dp[i, k, s];
            }
        }
    }
 
    // Stores the sum of dp[n,j,K*j]
    // all possible values of j with
    // average K and sum K * j
    int cnt = 0;
 
    // Iterate over the range [1, N]
    for (int j = 1; j <= n; j++)
    {
        cnt += dp[n, j, K * j];
    }
 
    // Return the readonly count
    return cnt;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 9, 7, 8, 9 };
    int K = 8;
    int N = arr.Length;
    Console.Write(countAverage(N, K, arr));
}
}
 
// This code is contributed by 29AjayKumar


输出:
5

时间复杂度: O(S * N 2 )其中S是数组的总和。
辅助空间: O(S * N 2 )