Longest Decreasing Subsequence
Last Updated :
11 Jul, 2025
Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in strictly decreasing order.
Examples:
Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85]
Output: 3
Explanation: The longest decreasing subsequence is {63, 55, 46}
Input: arr[] = {50, 3, 10, 7, 40, 80}
Output: 3
Explanation: The longest decreasing subsequence is {50, 10, 7}
The problem can be solved using Dynamic Programming
Optimal Substructure:
Let arr[0...n-1] be the input array and lds[i] be the length of the LDS ending at index i such that arr[i] is the last element of the LDS.
Then, lds[i] can be recursively written as:
lds[i] = 1 + max(lds[j]) where i > j > 0 and arr[j] > arr[i] or
lds[i] = 1, if no such j exists.
To find the LDS for a given array, we need to return max(lds[i]) where n > i > 0.
C++
// CPP program to find the length of the
// longest decreasing subsequence
#include <bits/stdc++.h>
using namespace std;
// Function that returns the length
// of the longest decreasing subsequence
int lds(int arr[], int n)
{
int lds[n];
int i, j, max = 0;
// Initialize LDS with 1 for all index
// The minimum LDS starting with any
// element is always 1
for (i = 0; i < n; i++)
lds[i] = 1;
// Compute LDS from every index
// in bottom up manner
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] && lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
// Select the maximum
// of all the LDS values
for (i = 0; i < n; i++)
if (max < lds[i])
max = lds[i];
// returns the length of the LDS
return max;
}
// Driver Code
int main()
{
int arr[] = { 15, 27, 14, 38, 63, 55, 46, 65, 85 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Length of LDS is " << lds(arr, n);
return 0;
}
Java
// Java program to find the
// length of the longest
// decreasing subsequence
import java.io.*;
class GFG
{
// Function that returns the
// length of the longest
// decreasing subsequence
static int lds(int arr[], int n)
{
int lds[] = new int[n];
int i, j, max = 0;
// Initialize LDS with 1
// for all index. The minimum
// LDS starting with any
// element is always 1
for (i = 0; i < n; i++)
lds[i] = 1;
// Compute LDS from every
// index in bottom up manner
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] &&
lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
// Select the maximum
// of all the LDS values
for (i = 0; i < n; i++)
if (max < lds[i])
max = lds[i];
// returns the length
// of the LDS
return max;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 15, 27, 14, 38,
63, 55, 46, 65, 85 };
int n = arr.length;
System.out.print("Length of LDS is " +
lds(arr, n));
}
}
// This code is contributed by anuj_67.
Python 3
# Python 3 program to find the length of
# the longest decreasing subsequence
# Function that returns the length
# of the longest decreasing subsequence
def lds(arr, n):
lds = [0] * n
max = 0
# Initialize LDS with 1 for all index
# The minimum LDS starting with any
# element is always 1
for i in range(n):
lds[i] = 1
# Compute LDS from every index
# in bottom up manner
for i in range(1, n):
for j in range(i):
if (arr[i] < arr[j] and
lds[i] < lds[j] + 1):
lds[i] = lds[j] + 1
# Select the maximum
# of all the LDS values
for i in range(n):
if (max < lds[i]):
max = lds[i]
# returns the length of the LDS
return max
# Driver Code
if __name__ == "__main__":
arr = [ 15, 27, 14, 38,
63, 55, 46, 65, 85 ]
n = len(arr)
print("Length of LDS is", lds(arr, n))
# This code is contributed by ita_c
C#
// C# program to find the
// length of the longest
// decreasing subsequence
using System;
class GFG
{
// Function that returns the
// length of the longest
// decreasing subsequence
static int lds(int []arr, int n)
{
int []lds = new int[n];
int i, j, max = 0;
// Initialize LDS with 1
// for all index. The minimum
// LDS starting with any
// element is always 1
for (i = 0; i < n; i++)
lds[i] = 1;
// Compute LDS from every
// index in bottom up manner
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] &&
lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
// Select the maximum
// of all the LDS values
for (i = 0; i < n; i++)
if (max < lds[i])
max = lds[i];
// returns the length
// of the LDS
return max;
}
// Driver Code
public static void Main ()
{
int []arr = { 15, 27, 14, 38,
63, 55, 46, 65, 85 };
int n = arr.Length;
Console.Write("Length of LDS is " +
lds(arr, n));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to find the
// length of the longest
// decreasing subsequence
// Function that returns the
// length of the longest
// decreasing subsequence
function lds($arr, $n)
{
$lds = array();
$i; $j; $max = 0;
// Initialize LDS with 1
// for all index The minimum
// LDS starting with any
// element is always 1
for ($i = 0; $i < $n; $i++)
$lds[$i] = 1;
// Compute LDS from every
// index in bottom up manner
for ($i = 1; $i < $n; $i++)
for ($j = 0; $j < $i; $j++)
if ($arr[$i] < $arr[$j] and
$lds[$i] < $lds[$j] + 1)
{
$lds[$i] = $lds[$j] + 1;
}
// Select the maximum
// of all the LDS values
for ($i = 0; $i < $n; $i++)
if ($max < $lds[$i])
$max = $lds[$i];
// returns the length
// of the LDS
return $max;
}
// Driver Code
$arr = array(15, 27, 14, 38, 63,
55, 46, 65, 85);
$n = count($arr);
echo "Length of LDS is " ,
lds($arr, $n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find the
// length of the longest
// decreasing subsequence
// Function that returns the
// length of the longest
// decreasing subsequence
function lds(arr,n)
{
let lds = new Array(n);
let i, j, max = 0;
// Initialize LDS with 1
// for all index. The minimum
// LDS starting with any
// element is always 1
for (i = 0; i < n; i++)
lds[i] = 1;
// Compute LDS from every
// index in bottom up manner
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] &&
lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
// Select the maximum
// of all the LDS values
for (i = 0; i < n; i++)
if (max < lds[i])
max = lds[i];
// returns the length
// of the LDS
return max;
}
// Driver Code
let arr=[15, 27, 14, 38,
63, 55, 46, 65, 85 ];
let n = arr.length;
document.write("Length of LDS is " +
lds(arr, n));
// This code is contributed by rag2127
</script>
Output : Length of LDS is 3
Time Complexity: O(n2)
Auxiliary Space: O(n)
Related Article: https://www.geeksforgeeks.org/dsa/longest-increasing-subsequence-dp-3/
Longest Decreasing Subsequence in O(n*log(n)) :
Another approach to finding the Longest Decreasing Subsequence is using the Longest Increasing Subsequence approach in n*log(n) complexity. Here is the link to find the details of the approach with O(n*log(n)) complexity https://www.geeksforgeeks.org/dsa/longest-monotonically-increasing-subsequence-size-n-log-n/
We can observe that the length of the Longest Decreasing Subsequence of any array is same as the length of the Longest Increasing Subsequence of that array if we multiply all elements by -1.
For example:
arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85]
then the length of longest decreasing subsequence of this array is same as length of longest increasing subsequence of arr[]=[-15, -27, -14, -38, -63, -55, -46, -65, -85]
In both cases the length is 3.
Steps to solve this problem:
1. Check if array is zero than return zero.
2. Declare a vector tail of array size.
3. Declare a variable length=1.
4. Initialize tail[0]=v[0].
5. Iterate through i=1 till size of array:
*Initialize auto b=tail.begin and e=tail.begin+length.
*Initialize auto it to lower bound of v[i].
*Check if it is equal to tail.begin+length than tail[length++]=v[i].
*Else update value at it =v[i].
6. Return length.
Below is the code of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to find longest Longest Increasing Subsequence Length
int LongestIncreasingSubsequenceLength(vector<int>& v)
{
if (v.size() == 0) // boundary case
return 0;
vector<int> tail(v.size(), 0);
int length = 1; // always points empty slot in tail
tail[0] = v[0];
for (int i = 1; i < v.size(); i++) {
// Do binary search for the element in
// the range from begin to begin + length
auto b = tail.begin(), e = tail.begin() + length;
auto it = lower_bound(b, e, v[i]);
// If not present change the tail element to v[i]
if (it == tail.begin() + length)
tail[length++] = v[i];
else
*it = v[i];
}
return length;
}
int main()
{
vector<int> v{ 15, 27, 14, 38, 63, 55, 46, 65, 85 };
int n=v.size();
// Making all elements negative as
// Longest Decreasing Subsequence of any array is
// same as longest Increasing Subsequence
// of negative values of that array.
for(int i=0;i<n;i++)
{
v[i]=-1*v[i];
}
cout
<< "Length of Longest Decreasing Subsequence is "
<< LongestIncreasingSubsequenceLength(v);
return 0;
}
// This code is contributed by Pushpesh Raj.
Java
// Java code for above approach
import java.io.*;
import java.lang.Math;
import java.util.*;
class LIS {
// Function to find longest Longest Increasing Subsequence Length
static int LongestIncreasingSubsequenceLength(int v[])
{
if (v.length == 0) // boundary case
return 0;
int[] tail = new int[v.length];
int length = 1; // always points empty slot in tail
tail[0] = v[0];
for (int i = 1; i < v.length; i++) {
if (v[i] > tail[length - 1]) {
// v[i] extends the largest subsequence
tail[length++] = v[i];
}
else {
// v[i] will extend a subsequence and
// discard older subsequence
// find the largest value just smaller than
// v[i] in tail
// to find that value do binary search for
// the v[i] in the range from begin to 0 +
// length
int idx = Arrays.binarySearch(
tail, 0, length - 1, v[i]);
// binarySearch in java returns negative
// value if searched element is not found in
// array
// this negative value stores the
// appropriate place where the element is
// supposed to be stored
if (idx < 0)
idx = -1 * idx - 1;
// replacing the existing subsequence with
// new end value
tail[idx] = v[i];
}
}
return length;
}
// Driver program to test above function
public static void main(String[] args)
{
int v[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
int n=v.length;
// Making all elements negative as
// Longest Decreasing Subsequence of any array is
// same as longest Increasing Subsequence
// of negative values of that array.
for(int i=0;i<n;i++)
{
v[i]=-1*v[i];
}
System.out.println(
"Length of Longest Decreasing Subsequence is "
+ LongestIncreasingSubsequenceLength(v));
}
}
// This code is contributed by Aman Kumar
Python3
from bisect import bisect_left
# Function to find longest Longest Increasing Subsequence Length
def LongestIncreasingSubsequenceLength(v):
n = len(v)
if n == 0: # boundary case
return 0
tail = [0] * n # vector to store the tail elements
length = 1 # always points empty slot in tail
tail[0] = v[0]
for i in range(1, n):
# Do binary search for the element in the range from begin to begin + length
j = bisect_left(tail, v[i], 0, length)
# If not present change the tail element to v[i]
if j == length:
tail[length] = v[i]
length += 1
else:
tail[j] = v[i]
return length
v = [15, 27, 14, 38, 63, 55, 46, 65, 85]
v = [-x for x in v] # Making all elements negative
print("Length of Longest Decreasing Subsequence is", LongestIncreasingSubsequenceLength(v))
# This code is contributed by unstoppablepandu.
C#
using System;
using System.Linq;
class LIS {
// Function to find longest Longest Increasing
// Subsequence Length
static int LongestIncreasingSubsequenceLength(int[] v)
{
if (v.Length == 0) // boundary case
return 0;
int[] tail = new int[v.Length];
int length = 1; // always points empty slot in tail
tail[0] = v[0];
for (int i = 1; i < v.Length; i++) {
if (v[i] > tail[length - 1]) {
// v[i] extends the largest subsequence
tail[length++] = v[i];
}
else {
// v[i] will extend a subsequence and
// discard older subsequence
// find the largest value just smaller than
// v[i] in tail
// to find that value do binary search for
// the v[i] in the range from begin to 0 +
// length
int idx = Array.BinarySearch(
tail, 0, length - 1, v[i]);
// binarySearch in C# returns negative
// value if searched element is not found in
// array
// this negative value stores the
// appropriate place where the element is
// supposed to be stored
if (idx < 0)
idx = ~idx;
// replacing the existing subsequence with
// new end value
tail[idx] = v[i];
}
}
return length;
}
// Driver program to test above function
public static void Main(string[] args)
{
int[] v = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
int n = v.Length;
// Making all elements negative as
// Longest Decreasing Subsequence of any array is
// same as longest Increasing Subsequence
// of negative values of that array.
for (int i = 0; i < n; i++) {
v[i] = -1 * v[i];
}
Console.WriteLine(
"Length of Longest Decreasing Subsequence is "
+ LongestIncreasingSubsequenceLength(v));
}
}
JavaScript
//JavaScript program to implement above approach.
function LongestIncreasingSubsequenceLength(v) {
if (v.length === 0) { // boundary case
return 0;
}
const tail = new Array(v.length).fill(0);
let length = 1; // always points empty slot in tail
tail[0] = v[0];
for (let i = 1; i < v.length; i++) {
// Do binary search for the element in
// the range from begin to begin + length
let b = 0, e = length;
let mid;
while (b < e) {
mid = Math.floor((b + e) / 2);
if (tail[mid] < v[i]) {
b = mid + 1;
} else {
e = mid;
}
}
// If not present change the tail element to v[i]
if (b === length) {
tail[length++] = v[i];
} else {
tail[b] = v[i];
}
}
return length;
}
let v = [15, 27, 14, 38, 63, 55, 46, 65, 85];
let n = v.length;
// Making all elements negative as
// Longest Decreasing Subsequence of any array is
// same as longest Increasing Subsequence
// of negative values of that array.
for (let i = 0; i < n; i++) {
v[i] = -v[i];
}
console.log("Length of Longest Decreasing Subsequence is " +
LongestIncreasingSubsequenceLength(v));
OutputLength of Longest Decreasing Subsequence is 3
Time Complexity: O(n*logn)
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem