The Wayback Machine - https://web.archive.org/web/20240917122226/https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/
Open In App

Longest Increasing Subsequence (LIS)

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array arr[] of size N, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.

Examples:            

Input: arr[] = {3, 10, 2, 1, 20}
Output: 3
Explanation: The longest increasing subsequence is 3, 10, 20

Input: arr[] = {30, 20, 10}
Output:1
Explanation: The longest increasing subsequences are {30}, {20} and (10)

Input: arr[] = {2, 2, 2}
Output: 1
Explanation: We consider only strictly increasing.

Input: arr[] = {10, 20, 35, 80}
Output: 4
Explanation: The whole array is sorted

Using Recursion:

The idea to do traverse the input array from left to right and find length of the Longest Increasing Subsequence (LIS) ending with every element arr[i]. Let the length found for arr[i] be L[i]. At the end we return maximum of all L[i] values. Now the question arises, how do we compute L[i]? For this, we us recursion, we consider all smaller elements on left of arr[i], recursively compute LIS value for all the smaller elements on left, take the maximum of all and add 1 to it. If there is no smaller element on left of an element, we return 1.

Let L(i) be the length of the LIS ending at index i such that arr[i] is the last element of the LIS. Then, L(i) can be recursively written as: 

  • L(i) = 1 + max(L(prev) ) where 0 < prev < i and arr[prev] < arr[i]; or
  • L(i) = 1, if no such prev exists.

Formally, the length of LIS ending at index i, is 1 greater than the maximum of lengths of all LIS ending at some index prev such that arr[prev] < arr[i] where prev < i.

After we fill the L array, we find LIS as maximum of all in L[]

Overall LIS = max(L[i]) where 0 <= i < n

We can see that the above recurrence relation follows the optimal substructure property. Follow the below illustration to see overlapping subproblems.

Consider arr[] = {3, 10, 2, 11}

L(i): Denotes LIS of subarray ending at position ‘i’

Recursion Tree

Recursion Tree

Follow the steps mentioned below to implement the above idea:

  1. We find LIS ending with i for all indexes from 1 to n-1. We us recursion here.
  2. We return maximum of LIS values found in the above step 1.

Below is the implementation of the recursive approach:

C++
#include <bits/stdc++.h>
using namespace std;

// Returns LIS of subarray ending with index i.
int lisEndAtI(vector<int>& arr, int i)
{
    // Base case
    if (i == 0)
        return 1;

    // Consider all elements on the left of i,
    // recursively compute LISs ending with 
    // them and consider the largest
    int mx = 1;
    for (int prev = 0; prev < i; prev++)
        if (arr[prev] < arr[i])
            mx = max(mx, lisEndAtI(arr, prev) + 1);
    return mx;
}

int lis(vector<int>& arr)
{
    int n = arr.size();
    int res = 1;
    for (int i = 1; i < n; i++)
        res = max(res, lisEndAtI(arr, i));
    return res;
}

// Driver program to test above function
int main()
{
    vector<int> arr = { 10, 22, 9, 33, 21, 50, 41, 60 };
    cout << "Length of lis is " << lis(arr);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// Utility function to find maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }

// Returns LIS of subarray ending with index i.
int lisEndAtI(int arr[], int i)
{
    // Base case
    if (i == 0)
        return 1;

    // Consider all elements on left of i,
    // recursively compute LISs ending with 
    // them and consider the largest
    int mx = 1;
    for (int prev = 0; prev < i; prev++)
        if (arr[prev] < arr[i])
            mx = max(mx, lisEndAtI(arr, prev) + 1);
    return mx;
}

int lis(int arr[], int n)
{
    int res = 1;
    for (int i = 1; i < n; i++)
        res = max(res, lisEndAtI(arr, i));
    return res;
}

// Driver program to test above function
int main()
{
    int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Length of lis is %d\n", lis(arr, n));
    return 0;
}
Java
import java.util.*;

class GfG {
    // Returns LIS of subarray ending with index i.
    static int lisEndAtI(int[] arr, int i) {
        // Base case
        if (i == 0)
            return 1;

        // Consider all elements on left of i,
        // recursively compute LISs ending with 
        // them and consider the largest
        int mx = 1;
        for (int prev = 0; prev < i; prev++)
            if (arr[prev] < arr[i])
                mx = Math.max(mx, lisEndAtI(arr, prev) + 1);
        return mx;
    }

    static int lis(int[] arr) {
        int n = arr.length;
        int res = 1;
        for (int i = 1; i < n; i++)
            res = Math.max(res, lisEndAtI(arr, i));
        return res;
    }

    // Driver program to test above function
    public static void main(String[] args) {
        int[] arr = {10, 22, 9, 33, 21, 50, 41, 60};

        // Function call
        System.out.println("Length of lis is " + lis(arr));
    }
}
Python
def lis_end_at_i(arr, i):
    # Base case
    if i == 0:
        return 1

    # Consider all elements on the left of i,
    # recursively compute LISs ending with 
    # them and consider the largest
    mx = 1
    for prev in range(i):
        if arr[prev] < arr[i]:
            mx = max(mx, lis_end_at_i(arr, prev) + 1)
    return mx

def lis(arr):
    n = len(arr)
    res = 1
    for i in range(1, n):
        res = max(res, lis_end_at_i(arr, i))
    return res

# Driver program to test the above function
arr = [10, 22, 9, 33, 21, 50, 41, 60]
print("Length of lis is", lis(arr))
C#
using System;

class GfG {
    // Returns LIS of subarray ending with index i.
    static int LisEndAtI(int[] arr, int i) {
        // Base case
        if (i == 0)
            return 1;

        // Consider all elements on left of i,
        // recursively compute LISs ending with 
        // them and consider the largest
        int mx = 1;
        for (int prev = 0; prev < i; prev++)
            if (arr[prev] < arr[i])
                mx = Math.Max(mx, LisEndAtI(arr, prev) + 1);
        return mx;
    }

    static int Lis(int[] arr) {
        int n = arr.Length;
        int res = 1;
        for (int i = 1; i < n; i++)
            res = Math.Max(res, LisEndAtI(arr, i));
        return res;
    }

    // Driver program to test above function
    static void Main() {
        int[] arr = { 10, 22, 9, 33, 21, 50, 41, 60 };

        // Function call
        Console.WriteLine("Length of lis is " + Lis(arr));
    }
}
JavaScript
function lisEndAtI(arr, i) {
    // Base case
    if (i === 0) {
        return 1;
    }

    // Consider all elements on the left of i,
    // recursively compute LISs ending with 
    // them and consider the largest
    let mx = 1;
    for (let prev = 0; prev < i; prev++) {
        if (arr[prev] < arr[i]) {
            mx = Math.max(mx, lisEndAtI(arr, prev) + 1);
        }
    }
    return mx;
}

function lis(arr) {
    const n = arr.length;
    let res = 1;
    for (let i = 1; i < n; i++) {
        res = Math.max(res, lisEndAtI(arr, i));
    }
    return res;
}

// Driver code to test the above function
const arr = [10, 22, 9, 33, 21, 50, 41, 60];
console.log("Length of lis is " + lis(arr));

Output
Length of lis is 5

Time Complexity: Exponential
Auxiliary Space: O(n) for recursion call stack

Using Memoization:

If notice carefully, we can see that the above recursive function lisEndAtI() also follows the overlapping subproblems property i.e., same substructure solved again and again in different recursion call paths. We can avoid this using the memoization approach. Since there is only one parameter that changes in recursive calls and the range of the parameter goes from 0 to n-1, so we us a 1D array of size n and initialize it as -1 to indicate that the values are not computed.

Below is the implementation of the above approach.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to compute LIS ending with index
// i using memoization
int lisEndAtI(vector<int>& arr, int i, vector<int>& dp)
{
    // Base case
    if (i == 0)
        return 1;

    // Check if the result is already computed
    if (dp[i] != -1)
        return dp[i];

    // Consider all elements on left of i,
    // recursively compute LISs ending with 
    // them and consider the largest
    int mx = 1;
    for (int prev = 0; prev < i; prev++)
        if (arr[prev] < arr[i])
            mx = max(mx, lisEndAtI(arr, prev, dp) + 1);

    // Store the result in the dp array
    dp[i] = mx;
    return dp[i];
}

int lis(vector<int>& arr)
{
    int n = arr.size();
  
    // Initialize memoization array with -1  
    vector<int> dp(n, -1);
  
    int res = 1;
    for (int i = 1; i < n; i++)
        res = max(res, lisEndAtI(arr, i, dp));
    return res;
}

// Driver program to test above function
int main()
{
    vector<int> arr = { 10, 22, 9, 33, 21, 50, 41, 60 };
    cout << "Length of lis is " << lis(arr);
    return 0;
}
C
#include <stdio.h>
#include <string.h>

int max(int a, int b) { return (a > b) ? a : b; }

// Function to compute LIS ending with 
// index i using memoization
int lisEndAtI(int arr[], int i, int dp[])
{
    // Base case
    if (i == 0)
        return 1;

    // Check if the result is already computed
    if (dp[i] != -1)
        return dp[i];

    // Consider all elements on the left of i,
    // recursively compute LISs ending with
    // them and consider the largest
    int mx = 1;
    for (int prev = 0; prev < i; prev++)
        if (arr[prev] < arr[i])
            mx = max(mx, lisEndAtI(arr, prev, dp) + 1);

    // Store the result in the dp array
    dp[i] = mx;
    return dp[i];
}

int lis(int arr[], int n)
{
    int dp[n];
  
    // Initialize memoization array with -1
    memset(dp, -1, sizeof(dp));
  
    int res = 1;
    for (int i = 1; i < n; i++)
        res = max(res, lisEndAtI(arr, i, dp));
    return res;
}

int main()
{
    int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Length of lis is %d\n", lis(arr, n));
    return 0;
}
Java
class GfG {

    // Function to compute LIS ending with index
    // i using memoization
    static int lisEndAtI(int arr[], int i, int dp[]) {
        // Base case
        if (i == 0)
            return 1;

        // Check if the result is already computed
        if (dp[i] != -1)
            return dp[i];

        // Consider all elements on the left of i,
        // recursively compute LISs ending with 
        // them and consider the largest
        int mx = 1;
        for (int prev = 0; prev < i; prev++)
            if (arr[prev] < arr[i])
                mx = Math.max(mx, lisEndAtI(arr, prev, dp) + 1);

        // Store the result in the dp array
        dp[i] = mx;
        return dp[i];
    }

    static int lis(int arr[], int n) {
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) {
            dp[i] = -1;
        }
        int res = 1;
        for (int i = 1; i < n; i++)
            res = Math.max(res, lisEndAtI(arr, i, dp));
        return res;
    }

    public static void main(String[] args) {
        int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
        int n = arr.length;
        System.out.println("Length of lis is " + lis(arr, n));
    }
}
Python
def lis_end_at_i(arr, i, dp):
  
    # Base case
    if i == 0:
        return 1

    # Check if the result is already computed
    if dp[i] != -1:
        return dp[i]

    # Consider all elements on the left of i,
    # recursively compute LISs ending with 
    # them and consider the largest
    mx = 1
    for prev in range(i):
        if arr[prev] < arr[i]:
            mx = max(mx, lis_end_at_i(arr, prev, dp) + 1)

    # Store the result in the dp array
    dp[i] = mx
    return dp[i]

def lis(arr):
    n = len(arr)
    dp = [-1] * n  # Initialize memo array with -1
    res = 1
    for i in range(1, n):
        res = max(res, lis_end_at_i(arr, i, dp))
    return res

# Driver program to test above function
if __name__ == "__main__":
    arr = [10, 22, 9, 33, 21, 50, 41, 60]
    print("Length of lis is", lis(arr))
C#
using System;

public class GfG
{
    // Function to compute LIS ending with index i
    // using memoization
    static int LisEndAtI(int[] arr, int i, int[] dp)
    {
        // Base case
        if (i == 0)
            return 1;

        // Check if the result is already computed
        if (dp[i] != -1)
            return dp[i];

        // Consider all elements on the left of i,
        // recursively compute LISs ending with 
        // them and consider the largest
        int mx = 1;
        for (int prev = 0; prev < i; prev++)
        {
            if (arr[prev] < arr[i])
            {
                mx = Math.Max(mx, LisEndAtI(arr, prev, dp) + 1);
            }
        }

        // Store the result in the dp array
        dp[i] = mx;
        return dp[i];
    }

    static int Lis(int[] arr, int n)
    {
        int[] dp = new int[n];
        for (int i = 0; i < n; i++)
        {
            dp[i] = -1;
        }

        int res = 1;
        for (int i = 1; i < n; i++)
        {
            res = Math.Max(res, LisEndAtI(arr, i, dp));
        }

        return res;
    }

    // Driver program to test above function
    public static void Main()
    {
        int[] arr = { 10, 22, 9, 33, 21, 50, 41, 60 };
        int n = arr.Length;

        // Function call
        Console.WriteLine("Length of lis is " + Lis(arr, n));
    }
}
JavaScript
function lisEndAtI(arr, i, dp) {
    // Base case
    if (i === 0) {
        return 1;
    }

    // Check if the result is already computed
    if (dp[i] !== -1) {
        return dp[i];
    }

    // Consider all elements on the left of i,
    // recursively compute LISs ending with
    // them and consider the largest
    let mx = 1;
    for (let prev = 0; prev < i; prev++) {
        if (arr[prev] < arr[i]) {
            mx = Math.max(mx, lisEndAtI(arr, prev, dp) + 1);
        }
    }

    // Store the result in the dp array
    dp[i] = mx;
    return dp[i];
}

function lis(arr) {
    const n = arr.length;
    const dp = new Array(n).fill(-1); // Initialize memo with -1
    let res = 1;
    for (let i = 1; i < n; i++) {
        res = Math.max(res, lisEndAtI(arr, i, dp));
    }
    return res;
}

const arr = [10, 22, 9, 33, 21, 50, 41, 60];
console.log("Length of lis is " + lis(arr));

Output
Length of lis is 3

Time Complexity: O(N2)
Auxiliary Space: O(N2)

Using Dynamic Programming:

If notice carefully, we can see that the above recursive function lisEndAtI() also follows the overlapping subproblems property i.e., same substructure solved again and again in different recursion call paths. We can avoid this using the memoization approach. Since there is only one parameter that changes in recursive calls and the range of the parameter goes from 0 to n-1, so we us a 1D array of size n.

Below is the implementation of the above approach:  

C++
#include <bits/stdc++.h>
using namespace std;

// lis() returns the length of the longest
// increasing subsequence in arr of size n
int lis(vector<int>& arr)
{
    int n = arr.size();
    vector<int> lis(n, 1);

    // Compute optimized LIS values in
    // bottom-up manner
    for (int i = 1; i < n; i++) {
        for (int prev = 0; prev < i; prev++) {
            if (arr[i] > arr[prev] && lis[i] < lis[prev] + 1) {
                lis[i] = lis[prev] + 1;
            }
        }
    }

    // Return maximum value in lis
    return *max_element(lis.begin(), lis.end());
}

// Driver program to test the above function
int main()
{
    vector<int> arr = { 10, 22, 9, 33, 21, 50, 41, 60 };
    cout << "Length of lis is " << lis(arr) << endl;
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

// lis() returns the length of the longest
// increasing subsequence in arr of size n
int lis(const int arr[], int n)
{
    int lis[n];
    for (int i = 0; i < n; i++) {
        lis[i] = 1;
    }

    // Compute optimized LIS values in
    // bottom-up manner
    for (int i = 1; i < n; i++) {
        for (int prev = 0; prev < i; prev++) {
            if (arr[i] > arr[prev] && lis[i] < lis[prev] + 1) {
                lis[i] = lis[prev] + 1;
            }
        }
    }

    // Find the maximum value in lis
    int max = lis[0];
    for (int i = 1; i < n; i++) {
        if (lis[i] > max) {
            max = lis[i];
        }
    }

    return max;
}

// Driver program to test the above function
int main()
{
    int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // Function call
    printf("Length of lis is %d\n", lis(arr, n));
    return 0;
}
Java
// Dynamic Programming Java implementation
// of LIS problem

import java.lang.*;

class GfG {

    // lis() returns the length of the longest
    // increasing subsequence in arr[] of size n
    static int lis(int arr[], int n)
    {
        int lis[] = new int[n];
      
        // Initialize LIS values for all indexes
        for (int i = 0; i < n; i++)
            lis[i] = 1;

        // Compute optimized LIS values in
        // bottom up manner
        for (int i = 1; i < n; i++)
            for (int prev = 0; prev < i; prev++)
                if (arr[i] > arr[prev] && lis[i] < lis[prev] + 1)
                    lis[i] = lis[prev] + 1;

        // Pick maximum of all LIS values
        int max = 1;
        for (int i = 0; i < n; i++)
            max = Math.max(max, lis[i]);

        return max;
    }

    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
        int n = arr.length;
        System.out.println("Length of lis is "
                           + lis(arr, n));
    }
}
Python
# Dynamic programming Python implementation
# of LIS problem

# lis returns length of the longest
# increasing subsequence in arr of size n
def lis(arr):
    n = len(arr)

    # Declare the list (array) for LIS and
    # initialize LIS values for all indexes
    lis = [1] * n

    # Compute optimized LIS values in bottom
    # -up manner
    for i in range(1, n):
        for prev in range(0, i):
            if arr[i] > arr[prev]:
                lis[i] = max(lis[i], lis[prev] + 1)

    # Return the maximum of all LIS values
    return max(lis)

# Driver program to test above function
if __name__ == '__main__':
    arr = [10, 22, 9, 33, 21, 50, 41, 60]
    print("Length of lis is", lis(arr))
C#
// Dynamic Programming C# implementation of LIS problem

using System;

class LIS {

    // lis() returns the length of the longest 
    // increasing subsequence in arr[] of size n
    static int lis(int[] arr, int n)
    {
        int[] lis = new int[n];

        // Initialize LIS values for all indexes
        for (int i = 0; i < n; i++)
            lis[i] = 1;

        // Compute optimized LIS values in bottom up manner
        for (int i = 1; i < n; i++)
        {
            for (int prev = 0; prev < i; prev++)
            {
                if (arr[i] > arr[prev] && lis[i] < lis[prev] + 1)
                    lis[i] = lis[prev] + 1;
            }
        }

        // Pick maximum of all LIS values        
        int max = 0;
        for (int i = 0; i < n; i++)
        {
            if (max < lis[i])
                max = lis[i];
        }

        return max;
    }

    // Driver code
    public static void Main()
    {
        int[] arr = { 10, 22, 9, 33, 21, 50, 41, 60 };
        int n = arr.Length;
        Console.WriteLine("Length of lis is " + lis(arr, n));
    }
}
JavaScript
// Dynamic Programming Javascript implementation
// of LIS problem 

// lis() returns the length of the longest
// increasing subsequence in arr[] of size n
function lis(arr, n) {
    let lis = Array(n).fill(0);
    let max = 0;

    // Initialize LIS values for all indexes
    for (let i = 0; i < n; i++)
        lis[i] = 1;

    // Compute optimized LIS values in
    // bottom up manner 
    for (let i = 1; i < n; i++) {
        for (let prev = 0; prev < i; prev++) {
            if (arr[i] > arr[prev] && lis[i] < lis[prev] + 1)
                lis[i] = lis[prev] + 1;
        }
    }

    // Pick maximum of all LIS values 
    for (let i = 0; i < n; i++)
        if (max < lis[i])
            max = lis[i];

    return max;
}

// Driver code
let arr = [10, 22, 9, 33, 21, 50, 41, 60];
let n = arr.length;
console.log("Length of lis is " + lis(arr, n) + "\n");

Output
Length of lis is 5

Time Complexity: O(N2) As a nested loop is used.
Auxiliary Space: O(N) Use of any array to store LIS values at each index.

We can solve this in O(n Log n) time using Binary Search. The idea is to traverse the given sequence and maintain a separate list of sorted subsequence so far. For every new element, find its position in the sorted subsequence using Binary Search. Please refer Longest Increasing Subsequence Size (N * logN) for details.



Previous Article
Next Article

Similar Reads

Longest Common Increasing Subsequence (LCS + LIS)
Prerequisites : LCS, LIS Given two arrays, find length of the longest common increasing subsequence [LCIS] and print one of such sequences (multiple sequences may exist)Suppose we consider two arrays - arr1[] = {3, 4, 9, 1} and arr2[] = {5, 3, 8, 9, 10, 2, 1}Our answer would be {3, 9} as this is the longest common subsequence which is increasing al
15+ min read
Minimizing and Maximizing longest Increasing Subsequence (LIS)
Given a string S of length n-1, consisting of characters '&lt;' and '&gt;' only. The ith character is the comparison result between the ith element and the (i+1)th element of the sequence. If the ith character of the string is '&lt;', then the ith element of the sequence is less than the (i+1)st element. If the ith character of the string is '&gt;'
9 min read
Longest Increasing Subsequence(LIS) using two arrays
Given two arrays A[] and B[] of size N. Your Task is to find Longest Increasing Subsequence(LIS) in another array C[] such that array C[] is formed by following rules: C[i] = A[i] or C[i] = B[i] for all i from 1 to N.Examples: Input: A[] = {2, 3, 1}, B[] = {1, 2, 1}Output: 2Explanation: Form C[] as following: For i = 1, we have two choices A[1] = 2
9 min read
Printing Longest Increasing Subsequence (LIS)
The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. Here we need to print a LIS as well. In case there are multiple LIS (as shown in the example 2 below), we need to print the LIS that has lexicographic ally sm
13 min read
Longest Increasing Subsequence using Longest Common Subsequence Algorithm
Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prerequisite: LCS, LISApproach: The longest increasing
12 min read
Length of Longest Increasing Subsequences (LIS) using Segment Tree
Given an array arr[] of size N, the task is to count the number of longest increasing subsequences present in the given array. Example: Input: arr[] = {2, 2, 2, 2, 2}Output: 5Explanation: The length of the longest increasing subsequence is 1, i.e. {2}. Therefore, count of longest increasing subsequences of length 1 is 5. Input: arr[] = {1, 3, 5, 4,
15+ min read
Construction of Longest Increasing Subsequence (N log N)
In my previous post, I have explained about longest increasing sub-sequence (LIS) problem in detail. However, the post only covered code related to querying size of LIS, but not the construction of LIS. I left it as an exercise. If you have solved, cheers. If not, you are not alone, here is code. If you have not read my previous post, read here. No
10 min read
Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
Given an array of random numbers, find the longest monotonically increasing subsequence (LIS) in the array. If you want to understand the O(NlogN) approach, it's explained very clearly here. In this post, a simple and time-saving implementation of O(NlogN) approach using stl is discussed. Below is the code for LIS O(NlogN): Implementation: C/C++ Co
5 min read
Longest Increasing Odd Even Subsequence
Given an array of size n. The problem is to find the length of the subsequence in the given array such that all the elements of the subsequence are sorted in increasing order and also they are alternately odd and even. Note that the subsequence could start either with the odd number or with the even number. Examples: Input : arr[] = {5, 6, 9, 4, 7,
8 min read
Printing longest Increasing consecutive subsequence
Given n elements, write a program that prints the longest increasing subsequence whose adjacent element difference is one. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 3 4 5 6 7 8 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6, 7, 8, 3, 4, 5, 9, 10} O
8 min read
Length of the longest increasing subsequence such that no two adjacent elements are coprime
Given an array arr[] of size N. The task is to find the length of the longest subsequence from the given array such that the sequence is strictly increasing and no two adjacent elements are coprime. Note: The elements in the given array are strictly increasing in order (1 &lt;= a[i] &lt;= 105) Examples: Input : a[] = { 1, 2, 3, 4, 5, 6} Output : 3
9 min read
Length of longest increasing subsequence in a string
Given a string S, the task is to find the length of the longest increasing subsequence present in the given string. A sequence of characters placed in increasing order of their ASCII values is called an increasing sequence. Examples: Input: S = "abcfgffs"Output: 6Explanation: Subsequence "abcfgs" is the longest increasing subsequence present in the
7 min read
Longest Increasing Subsequence using BIT
Given an array arr, the task is to find the length of The Longest Increasing Sequence using Binary Indexed Tree (BIT) Examples: Input: arr = {6, 5, 1, 3, 2, 4, 8, 7} Output: 4 Explanation: The possible subsequences are {1 2 4 7}, {1 2 4 8}, {1 3 4 8}, {1 3 4 7} Now insert the elements one by one from left to right: 6 is inserted: max length till 5
13 min read
Length of longest increasing index dividing subsequence
Given an array arr[] of size N, the task is to find the longest increasing sub-sequence such that index of any element is divisible by index of previous element (LIIDS). The following are the necessary conditions for the LIIDS:If i, j are two indices in the given array. Then: i &lt; jj % i = 0arr[i] &lt; arr[j] Examples: Input: arr[] = {1, 2, 3, 7,
7 min read
Longest Increasing Subsequence having sum value atmost K
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: 5 Explanation: If we select subsequence {0, 1, 3, 7, 15} then total sum will be 26, which is less than
5 min read
Longest increasing subsequence which forms a subarray in the sorted representation of the array
Given an array arr[] of N integers, the task is to find the length of the longest increasing subsequence such that it forms a subarray when the original array is sorted. Examples: Input: arr[] = { 2, 6, 4, 8, 2, 9 }Output: 3Explanation: Sorted array: {2, 2, 4, 6, 8, 9}All possible non-decreasing sequences of the original array are {2}, {6}, {4}, {8
11 min read
Maximize length of longest increasing prime subsequence from the given array
Given an array, arr[] of size N, the task is to find the length of the longest increasing prime subsequence possible by performing the following operations. If arr[i] is already a prime number, no need to update arr[i].Update non-prime arr[i] to the closest prime number less than arr[i].Update non-prime arr[i] to the closest prime number greater th
13 min read
Length of longest increasing prime subsequence from a given array
Given an array arr[] consisting of N positive integers, the task is to find the length of the longest increasing subsequence consisting of Prime Numbers in the given array. Examples: Input: arr[] = {1, 2, 5, 3, 2, 5, 1, 7}Output: 4Explanation:The Longest Increasing Prime Subsequence is {2, 3, 5, 7}.Therefore, the answer is 4. Input: arr[] = {6, 11,
9 min read
Length of longest increasing absolute even subsequence
Given an array arr[] consisting of N integers, the task is to find the length of the longest increasing absolute even subsequence. An increasing absolute even subsequence is an increasing subsequence of array elements having absolute difference between adjacent pairs as even. Examples: Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60} Output: 4Explana
7 min read
Length of the longest increasing subsequence which does not contain a given sequence as Subarray
Given two arrays arr[] and arr1[] of lengths N and M respectively, the task is to find the longest increasing subsequence of array arr[] such that it does not contain array arr1[] as subarray. Examples: Input: arr[] = {5, 3, 9, 3, 4, 7}, arr1[] = {3, 3, 7}Output: 4Explanation: Required longest increasing subsequence is {3, 3, 4, 7}. Input: arr[] =
14 min read
Longest increasing subsequence consisting of elements from indices divisible by previously selected indices
Given an array arr[] consisting of N positive integers, the task is to find the length of the longest increasing subsequence possible by selecting elements from indices that are divisible by all the previously selected indices. Note: Consider 1-based indexing Examples: Input: arr[] = {1, 4, 2, 3, 6, 4, 9}Output: 3Explanation: The optimal way is to
6 min read
Longest Non-Increasing Subsequence in a Binary String
Given a binary string S of size N, the task is to find the length of the longest non-increasing subsequence in the given string S. Examples: Input: S = "0101110110100001011"Output: 12 Explanation: The longest non-increasing subsequence is "111111100000", having length equal to 12. Input: S = 10101Output: 3 Approach: The given problem can be solved
8 min read
Longest subsequence from an array of pairs having first element increasing and second element decreasing.
Given an array of pairs A[][] of size N, the task is to find the longest subsequences where the first element is increasing and the second element is decreasing. Examples: Input: A[]={{1, 2}, {2, 2}, {3, 1}}, N = 3Output: 2Explanation: The longest subsequence satisfying the conditions is of length 2 and consists of {1, 2} and {3, 1}; Input: A[] = {
10 min read
Longest Increasing Subsequence in given Linked List
Given a sequence of numbers in the form of a linked list lis. Find the length of the Longest Increasing Subsequence(LIS) of the given Linked List. Examples: Input: list = 3-&gt;10-&gt;2-&gt;1-&gt;20Output: 3Explanation: The longest increasing subsequence is 3-&gt;10-&gt; 20 Input: list = 3-&gt; 2Output: 1Explanation: The longest increasing subseque
10 min read
Longest Increasing consecutive subsequence | Set-2
Given an array arr[] of N elements, the task is to find the length of the longest increasing subsequence whose adjacent element difference is one. Examples: Input: arr[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output: 6 Explanation: The subsequence {3, 4, 5, 6, 7, 8} is the longest increasing subsequence whose adjacent elements differs by one. Input: a
5 min read
Find the Second Longest Increasing Subsequence
Given an array arr[] of integers, the task is to find the length of the second longest increasing subsequence (LIS). Examples: Input: arr[] = {1, 2, 3, 4}Output: 3 Explanation: Here the length of LIS is 4 which is whole array and length of second LIS is 3 which can be any sequence, {1, 2, 3} or {1, 2, 4} or {1, 3, 4} or {2, 3, 4}. Input: arr[] = {1
15+ min read
Longest Increasing consecutive subsequence
Given N elements, write a program that prints the length of the longest increasing consecutive subsequence. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 6 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6, 7, 8, 3, 4, 5, 9, 10} Output : 5 Explanation: 6,
10 min read
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence. Examples: Input: arr[] = {4, 6, 1, 2, 3, 8} Output: 10 Explanation: Elements are 4 and 6 Input: arr[] = {5, 4, 3, 2, 1} Output: 14 Explanation: Elements are 5, 4, 3, 2 Recommended PracticeMaximum sum of elements
8 min read
Find the Longest Increasing Subsequence in Circular manner
Given an array, the task is to find LIS (Longest Increasing Subsequence) in a circular way. Examples : Input : arr[] = {5, 4, 3, 2, 1} Output : 2 Although there is no LIS in a given array but in a circular form there can be {1, 5}, {2, 5}, ...... Input : arr[]= {5, 6, 7, 1, 2, 3} Output : 6 {1, 2, 3, 5, 6, 7} will be the LIS in the circular manner.
11 min read
Longest Increasing Subsequence Size (N log N)
Given an array arr[] of size N, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order, in O(N log N). Examples: Input: arr[] = {3, 10, 2, 1, 20}Output: 3Explanation: The longest increasing subsequence is 3, 10, 20 Inpu
8 min read