Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Longest decreasing subsequence in 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.
Comment

PREVIOUS NEXT
Code Example
Java :: MOST COMPLEX NAME OF CONSUMER 
Java :: list in list 
Java :: Deque interface in Java 
Java :: power of a number in java 
Java :: how to create arraylist in java 
Java :: countdown timer with seekbar 
Java :: how to solve CopyBuffer from HiLo failed, no data 
Java :: android stop audio playing by activity lifecycle 
Java :: hanoi recursive algorithm 
Java :: Picked up _JAVA_OPTIONS: -Xmx256M intillij 
Java :: Get directory in android java 
Java :: jmonkey shapes 
Java :: how to install openjdk 16 
Java :: Iterating an Array Using While Loop 
Java :: set integer array value to null java 
Java :: computeifabsent hashmap java 
Java :: how-to-use-volley-string-request-in-android 
Java :: OCA Exam Questions 
Java :: goodbye java 
Java :: how to read json object from text file in java 
Java :: ex javaloop 
Java :: BasicAWSCredentials 
Java :: capitalize a letter in java 
Java :: create and populate list one line java 
Java :: java find nth largest element using priority queue heap 
Java :: How to efficiently solve the knpasack problem, in Java? 
Java :: codding loop 
Java :: how to find root viewGroop 
Java :: super class and concrete class in java 
Java :: java was started but returned exit code=13 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =