# Solution For Longest Subsequence With Limited Sum

Problem Statement:

Given an integer array nums, you need to find the longest subsequence that has a sum less than or equal to target. If there is no such subsequence, return 0.

Example 1:

Input: nums = [10,2,-2,-20,10], target = -10

Output: 2

Explanation: The longest subsequence is [2,-2] as the sum is -10.

Example 2:

Input: nums = [1,2,3,4,5], target = 11

Output: 5

Explanation: The entire array is the longest subsequence as the sum is less than or equal to target.

Approach:

This is dynamic programming problem. We will use the memoization technique. We will try to find the longest subsequence that has a sum less than or equal to target for each index one by one. We will first create a 2D array dp with size (nums.length+1) x (target+1) and will initialize it with 0s. Here dp[i][j] represents the longest subsequence that has a sum less than or equal to j till index i-1 (0-based indexing).

We will start filling the dp array by iterating the nums array from 1st index to last. For each index i we will iterate all the values from 1 to target and fill the dp[i][j]. There are two cases:

We can ignore the current element:

In this case the longest subsequence till index i will be same as the longest subsequence till index i-1. Hence, we just copy dp[i-1][j] to dp[i][j].We can include the current element:

In this case, we check if nums[i-1] <= j. If yes, then we check if including nums[i-1] will result in a longer subsequence than already we have. If yes, we update dp[i][j] with that length.

Finally, find the maximum length of subsequence computed from dp[][] array and return it.

Code:

public int longestSubsequence(int[] nums, int target) {

int n = nums.length;

int[][] dp = new int[n+1][target+1];

int res = 0;

```
for(int i=1; i<=n; i++) {
for(int j=1; j<=target; j++) {
if(nums[i-1] <= j) {
dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-nums[i-1]]+1);
} else {
dp[i][j] = dp[i-1][j];
}
res = Math.max(dp[i][j], res);
}
}
return res;
```

}

Complexity Analysis:

Time Complexity: O(n*target). The nested loops iterate n and target elements each.

Space Complexity: O(n*target). We use dp array of size n*target.

## Step by Step Implementation For Longest Subsequence With Limited Sum

The problem can be found here: https://leetcode.com/problems/longest-subsequence-with-limited-sum/ One possible solution is to use a dynamic programming approach. We can create an array dp where dp[i] represents the longest subsequence with limited sum that ends at index i. Then, we can iterate through the array, and for each element, we can consider adding it to the subsequence ending at the previous index if the sum of the subsequence does not exceed the limit. public int longestSubsequenceWithLimitedSum(int[] nums, int limit) { int n = nums.length; int[] dp = new int[n]; dp[0] = 1; int maxLen = 1; for (int i = 1; i < n; i++) { int len = 1; int sum = nums[i]; for (int j = i-1; j >= 0; j--) { if (sum + nums[j] <= limit) { len++; sum += nums[j]; } } dp[i] = len; maxLen = Math.max(maxLen, len); } return maxLen; }

def longestSubsequenceWithLimitedSum(arr, limit): # Initialize result n = len(arr) dp = [1]*n # Compute optimized subsequences in bottom up manner for i in range (1 , n): for j in range(0, i): if (arr[i] + arr[j] <= limit and dp[i] < dp[j] + 1): dp[i] = dp[j]+1 # Return maximum return dp[n-1]

var longestSubsequenceWithLimitedSum = function(arr, limit) { // your code here };

int longestSubsequenceWithLimitedSum(vector& nums, int limit) { int n = nums.size(); int dp[n]; for(int i = 0; i < n; i++) { dp[i] = 1; for(int j = 0; j < i; j++) { if(nums[i] - nums[j] <= limit && nums[i] >= nums[j]) { dp[i] = max(dp[i], dp[j] + 1); } } } return *max_element(dp, dp + n); }

int longestSubsequenceWithLimitedSum(int[] sequence, int limit) { int n = sequence.length; // dp[i] is going to store length of the // longest subsequence with sum <= limit // and last element as sequence[i]. int[] dp = new int[n]; // Fill dp[] using above recursive formula for (int i = 0; i < n; i++) dp[i] = 1; // Consider all subsequences and find // the maximum possible sum for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (sequence[i] >= sequence[j] && dp[i] < dp[j] + 1 && sequence[i] + sequence[j] <= limit) dp[i] = dp[j] + 1; // Find the maximum length subsequence int max = 0; for (int i = 0; i < n; i++) if (dp[i] > max) max = dp[i]; return max; }