给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是用平均K 计算给定数组的子序列数。
例子:
Input: arr[] = {9, 7, 8, 9}, K = 8
Output: 5
Explanation: The subsequences having average 8 are {8}, {9, 7}, {7, 9}, {9, 7, 8}, {7, 8, 9}.
Input: arr[] = {5, 5, 1}, K = 4
Output: 0
Explanation: No such subsequence can be obtained
朴素方法:解决问题的最简单方法是使用递归。请按照以下步骤解决问题:
- 每个数组元素有两种可能的选择,即在总和中包括当前元素或在总和中排除当前元素并增加每次递归调用的当前索引。
- 对于上述两种可能性,返回平均为K的子序列数。
- 基本情况是检查是否已到达最后一个索引。基本情况下的平均值可以通过将包含在该子序列中的数组元素的总和相除来计算。
- 如果平均值等于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转换状态:
If ith element is included, then dp[i + 1][k + 1][s + arr[i]] += dp[i][k][s]
If ith element is excluded, then dp[i + 1][k][s] += dp[i][k][s]
- 最后,答案将是所有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
Javascript
输出:
5
时间复杂度: O(S*N 2 ),其中 S 是数组的总和。
辅助空间: O(S*N 2 )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。