Longest Increasing Subsequence (LIS)
Last Updated :
16 Sep, 2024
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
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
Follow the steps mentioned below to implement the above idea:
- We find LIS ending with i for all indexes from 1 to n-1. We us recursion here.
- 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));
Time Complexity: Exponential
Auxiliary Space: O(n) for recursion call stack
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));
Time Complexity: O(N2)
Auxiliary Space: O(N2)
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");
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.
Using Binary Search:
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.
Related Articles :
Please Login to comment...