Related Articles

# Longest Increasing Subsequence having sum value atmost K

• Difficulty Level : Medium
• Last Updated : 17 May, 2021

Given an integer array arr[] of size N and an integer K. The task is to find the length of the 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:
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:

1. The above problem can be solved using recursion.
• Choose the element at that position if the 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++

 `// 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

 ``
Output:
`5`

Time Complexity: O (2N)
Auxiliary Space: O (1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up