Longest Decreasing Subsequence - GeeksforGeeks

11 May, 2021
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 ProgrammingOptimal 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++
Java
Python 3
C#
PHP
Javascript
// 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 subsequenceint 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 Codeint 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 program to find the// length of the longest// decreasing subsequenceimport java.io.*; class GFG{ // Function that returns the// length of the longest// decreasing subsequencestatic 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 Codepublic 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 program to find the length of# the longest decreasing subsequence # Function that returns the length# of the longest decreasing subsequencedef 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 Codeif __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# program to find the// length of the longest// decreasing subsequenceusing System; class GFG{ // Function that returns the// length of the longest// decreasing subsequencestatic 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 Codepublic 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 program to find the// length of the longest// decreasing subsequence  // Function that returns the// length of the longest// decreasing subsequencefunction 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.?>
<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>
Length of LDS is 3
 
Time Complexity: O(n2) Auxiliary Space: O(n)Related Article: https://www.geeksforgeeks.org/longest-increasing-subsequence/ 
vt_m
ukasp
rag2127
Algorithms-Dynamic Programming
Dynamic Programming
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Subset Sum Problem | DP-25
Coin Change | DP-7
Matrix Chain Multiplication | DP-8
Longest Palindromic Substring | Set 1
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Sieve of Eratosthenes
Overlapping Subproblems Property in Dynamic Programming | DP-1
Edit Distance | DP-5