📌  相关文章
📜  数组中总和小于或等于 X 的子序列计数

📅  最后修改于: 2021-09-17 07:25:01             🧑  作者: Mango

给定一个大小为N的整数数组arr[]和一个整数X ,任务是计算该数组中子序列的数量,使其总和小于或等于X
注意: 1 <= N <= 1000 和 1 <= X <= 1000,其中N是数组的大小。

例子:

朴素方法:生成数组的所有子序列并检查总和是否小于或等于 X。
时间复杂度: O(2 N )

有效方法:使用动态规划生成子序列的计数。为了解决问题,请按照以下步骤操作:

  • 对于任何索引ind ,如果arr[ind]X那么,子序列的计数包括以及排除当前索引处的元素:
  • 否则,计算不包括当前索引的子序列:
  • 最后,从函数返回的最终计数中减去 1,因为它也计算一个空子序列。

下面是上述方法的实现:

C++
// C++ Program to count number
// of subsequences in an array
// with sum less than or equal to X
#include 
using namespace std;
 
// Utility function to return the count
// of subsequence in an array with sum
// less than or equal to X
int countSubsequenceUtil(
    int ind, int sum,
    int* A, int N,
    vector >& dp)
{
    // Base condition
    if (ind == N)
        return 1;
 
    // Return if the sub-problem
    // is already calculated
    if (dp[ind][sum] != -1)
        return dp[ind][sum];
 
    // Check if the current element is
    // less than or equal to sum
    if (A[ind] <= sum) {
        // Count subsequences excluding
        // the current element
        dp[ind][sum]
            = countSubsequenceUtil(
                  ind + 1,
                  sum, A, N, dp)
              +
 
              // Count subsequences including
              // the current element
              countSubsequenceUtil(
                  ind + 1,
                  sum - A[ind],
                  A, N, dp);
    }
 
    else {
        // Exclude current element
        dp[ind][sum]
            = countSubsequenceUtil(
                ind + 1,
                sum, A,
                N, dp);
    }
 
    // Return the result
    return dp[ind][sum];
}
 
// Function to return the count of subsequence
// in an array with sum less than or equal to X
int countSubsequence(int* A, int N, int X)
{
    // Initialize a DP array
    vector > dp(
        N,
        vector(X + 1, -1));
 
    // Return the result
    return countSubsequenceUtil(0, X, A,
                                N, dp)
           - 1;
}
 
// Driver Code
int main()
{
    int arr[] = { 25, 13, 40 }, X = 50;
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << countSubsequence(arr, N, X);
 
    return 0;
}


Java
// Java program to count number
// of subsequences in an array
// with sum less than or equal to X
class GFG{
 
// Utility function to return the count
// of subsequence in an array with sum
// less than or equal to X
static int countSubsequenceUtil(int ind, int sum,
                                int []A, int N,
                                int [][]dp)
{
     
    // Base condition
    if (ind == N)
        return 1;
 
    // Return if the sub-problem
    // is already calculated
    if (dp[ind][sum] != -1)
        return dp[ind][sum];
 
    // Check if the current element is
    // less than or equal to sum
    if (A[ind] <= sum)
    {
         
        // Count subsequences excluding
        // the current element
        dp[ind][sum] = countSubsequenceUtil(
                           ind + 1, sum,
                           A, N, dp) +
                        
                       // Count subsequences
                       // including the current
                       // element
                       countSubsequenceUtil(
                           ind + 1,
                           sum - A[ind],
                           A, N, dp);
    }
    else
    {
         
        // Exclude current element
        dp[ind][sum] = countSubsequenceUtil(
                           ind + 1, sum,
                           A, N, dp);
    }
 
    // Return the result
    return dp[ind][sum];
}
 
// Function to return the count of subsequence
// in an array with sum less than or equal to X
static int countSubsequence(int[] A, int N, int X)
{
     
    // Initialize a DP array
    int [][]dp = new int[N][X + 1];
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < X + 1; j++)
        {
            dp[i][j] = -1;
        }
    }
     
    // Return the result
    return countSubsequenceUtil(0, X, A,
                                N, dp) - 1;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 25, 13, 40 }, X = 50;
    int N = arr.length;
 
    System.out.print(countSubsequence(arr, N, X));
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to count number
// of subsequences in an array
// with sum less than or equal to X
using System;
 
class GFG{
 
// Utility function to return the count
// of subsequence in an array with sum
// less than or equal to X
static int countSubsequenceUtil(int ind, int sum,
                                int []A, int N,
                                int [,]dp)
{
     
    // Base condition
    if (ind == N)
        return 1;
 
    // Return if the sub-problem
    // is already calculated
    if (dp[ind, sum] != -1)
        return dp[ind, sum];
 
    // Check if the current element is
    // less than or equal to sum
    if (A[ind] <= sum)
    {
         
        // Count subsequences excluding
        // the current element
        dp[ind, sum] = countSubsequenceUtil(
                           ind + 1, sum,
                           A, N, dp) +
                            
                       // Count subsequences
                       // including the current
                       // element
                       countSubsequenceUtil(
                           ind + 1,
                           sum - A[ind],
                           A, N, dp);
    }
    else
    {
         
        // Exclude current element
        dp[ind, sum] = countSubsequenceUtil(
                           ind + 1, sum,
                           A, N, dp);
    }
 
    // Return the result
    return dp[ind, sum];
}
 
// Function to return the count of subsequence
// in an array with sum less than or equal to X
static int countSubsequence(int[] A, int N, int X)
{
     
    // Initialize a DP array
    int [,]dp = new int[N, X + 1];
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < X + 1; j++)
        {
            dp[i, j] = -1;
        }
    }
     
    // Return the result
    return countSubsequenceUtil(0, X, A,
                                N, dp) - 1;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 25, 13, 40 };
    int X = 50;
    int N = arr.Length;
 
    Console.Write(countSubsequence(arr, N, X));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

时间复杂度: O(N*X)