Given an integer array arr[] of size N and an integer K. The task is to find length of longest subsequence whose sum is less than or equal to K.
Example:
Input: arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} K = 40
Output: 5
Explanation:
If we select subsequence {0, 1, 3, 7, 15} then total sum will be 26, which is less than 40. Hence, the longest increasing possible subsequence length is 5.
Input: arr[] = {5, 8, 3, 7, 9, 1} K = 4
Output: 1
Approach:
- The above problem can be solve using recursion.
-
- At any position in the array, there are two choices:
- Choose the element at that position if total sum is less than K and explore the rest items.
- Leave the element at that position and explore the rest.
Recurrence relation will be given as:
Recurrence relation:
T(N) = max(sove(arr, N, arr[i], i+1, K-arr[i])+1, solve(arr, N, prevele, i+1, K));
Base conditions:
if(i >= N || K <= 0)
return 0
Here is the implementation of the above approach:
// C++ program to find the Longest // Increasing Subsequence having sum // value atmost K #include <bits/stdc++.h> 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; } |
5
Time Complexity: O (2N)
Auxiliary Space: O (1)
Recommended Posts:
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest subsequence having difference atmost K
- Longest subsequence such that absolute difference between every pair is atmost 1
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence using BIT
- Longest Increasing Subsequence | DP-3
- Longest Increasing Subsequence Size (N log N)
- Longest Common Increasing Subsequence (LCS + LIS)
- C++ Program for Longest Increasing Subsequence
- Longest Increasing consecutive subsequence
- C/C++ Program for Longest Increasing Subsequence
- Construction of Longest Increasing Subsequence (N log N)
- Python program for Longest Increasing Subsequence
- Java Program for Longest Increasing Subsequence
- Printing longest Increasing consecutive subsequence
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- Find the Longest Increasing Subsequence in Circular manner
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
- Length of longest increasing index dividing subsequence
- Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


