📜  总和值最大为 K 的最长递增子序列

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

给定一个大小为N的整数数组arr[]和一个整数K 。任务是找到总和小于或等于K的最长子序列的长度。

例子:

方法:

  1. 上面的问题可以用递归来解决。
    • 如果总和小于K ,则选择该位置的元素并探索其余项目。
    • 将元素留在该位置并探索其余部分。

递归关系将被给出为:

下面是上述方法的实现:

C++
// C++ program to find the Longest
// Increasing Subsequence having sum
// value atmost K
#include 
using namespace std;
 
int solve(int arr[], int N,
          int prevele, int i, int K)
{
    // check for base cases
    if (i >= N || K <= 0)
        return 0;
 
    // check if it is possible to take
    // current elements
    if (arr[i] <= prevele
        || (K - arr[i] < 0)) {
 
        return solve(arr, N, prevele,
                     i + 1, K);
    }
 
    // if current element is ignored
    else {
        int ans = max(
            solve(arr, N, arr[i],
                  i + 1, K - arr[i])
                + 1,
            solve(arr, N, prevele,
                  i + 1, K));
        return ans;
    }
}
 
// Driver Code
int main()
{
    int N = 16;
    int arr[N]
        = { 0, 8, 4, 12,
            2, 10, 6, 14,
            1, 9, 5, 13,
            3, 11, 7, 15 };
    int K = 40;
 
    cout << solve(arr, N,
                  INT_MIN, 0, K)
         << endl;
}


Java
// Java program to find the Longest
// Increasing Subsequence having sum
// value atmost K
import java.io.*;
 
class GFG{
     
static int solve(int arr[], int N,
                 int prevele, int i, int K)
{
     
    // Check for base cases
    if (i >= N || K <= 0)
        return 0;
 
    // Check if it is possible to take
    // current elements
    if (arr[i] <= prevele ||
       (K - arr[i] < 0))
    {
        return solve(arr, N, prevele,
                     i + 1, K);
    }
 
    // If current element is ignored
    else
    {
        int ans = Math.max(solve(arr, N, arr[i],
                              i + 1, K - arr[i]) + 1,
                           solve(arr, N, prevele,
                                 i + 1, K));
                                  
        return ans;
    }
}
 
// Driver code
public static void main (String[] args)
{
    int N = 16;
    int arr[] = new int[]{ 0, 8, 4, 12,
                           2, 10, 6, 14,
                           1, 9, 5, 13,
                           3, 11, 7, 15 };
    int K = 40;
 
    System.out.print(solve(arr, N,
          Integer.MIN_VALUE, 0, K));
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program to find the Longest
# Increasing Subsequence having sum
# value atmost K
import sys
 
def solve(arr, N, prevele, i, K):
     
    # Check for base cases
    if (i >= N or K <= 0):
        return 0;
 
    # Check if it is possible to take
    # current elements
    if (arr[i] <= prevele or
       (K - arr[i] < 0)):
        return solve(arr, N, prevele,
                     i + 1, K);
 
    # If current element is ignored
    else:
        ans = max(solve(arr, N, arr[i],
                     i + 1, K - arr[i]) + 1,
                  solve(arr, N, prevele,
                        i + 1, K));
 
        return ans;
 
# Driver code
if __name__ == '__main__':
     
    N = 16;
    arr = [ 0, 8, 4, 12,
            2, 10, 6, 14,
            1, 9, 5, 13,
            3, 11, 7, 15 ];
    K = 40;
 
    print(solve(arr, N, -sys.maxsize, 0, K));
 
# This code is contributed by 29AjayKumar


C#
// C# program to find the Longest
// Increasing Subsequence having sum
// value atmost K
using System;
 
class GFG{
     
static int solve(int[] arr, int N,
                 int prevele, int i, int K)
{
     
    // Check for base cases
    if (i >= N || K <= 0)
        return 0;
 
    // Check if it is possible to take
    // current elements
    if (arr[i] <= prevele ||
       (K - arr[i] < 0))
    {
        return solve(arr, N, prevele,
                     i + 1, K);
    }
 
    // If current element is ignored
    else
    {
        int ans = Math.Max(solve(arr, N, arr[i],
                                 i + 1, K - arr[i]) + 1,
                           solve(arr, N, prevele,
                                 i + 1, K));
                                 
        return ans;
    }
}
 
// Driver code
public static void Main ()
{
    int N = 16;
    int[] arr = new int[]{ 0, 8, 4, 12,
                           2, 10, 6, 14,
                           1, 9, 5, 13,
                           3, 11, 7, 15 };
    int K = 40;
 
    Console.Write(solve(arr, N,
        Int32.MinValue, 0, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
5

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

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