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

📅  最后修改于: 2021-06-26 21:13:50             🧑  作者: 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)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。