Java Program to Find the Longest Increasing Subsequence

This is a Java Program to implement Longest Increasing Subsequence Algorithm. This program finds the longest increasing sequence of numbers from an array of numbers.

Here is the source code of the Java Program to implement Longest Increasing Subsequence Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **    Java Program to implement Longest Increasing Subsequence Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class  LongestIncreasingSubsequence **/
  8. public class  LongestIncreasingSubsequence
  9. {
  10.     /** function lis **/
  11.     public int[] lis(int[] X)
  12.     {        
  13.         int n = X.length - 1;
  14.         int[] M = new int[n + 1];  
  15.         int[] P = new int[n + 1]; 
  16.         int L = 0;
  17.  
  18.         for (int i = 1; i < n + 1; i++)
  19.         {
  20.             int j = 0;
  21.  
  22.             /** Linear search applied here. Binary Search can be applied too.
  23.                 binary search for the largest positive j <= L such that 
  24.                 X[M[j]] < X[i] (or set j = 0 if no such value exists) **/
  25.  
  26.             for (int pos = L ; pos >= 1; pos--)
  27.             {
  28.                 if (X[M[pos]] < X[i])
  29.                 {
  30.                     j = pos;
  31.                     break;
  32.                 }
  33.             }            
  34.             P[i] = M[j];
  35.             if (j == L || X[i] < X[M[j + 1]])
  36.             {
  37.                 M[j + 1] = i;
  38.                 L = Math.max(L,j + 1);
  39.             }
  40.         }
  41.  
  42.         /** backtrack **/
  43.  
  44.         int[] result = new int[L];
  45.         int pos = M[L];
  46.         for (int i = L - 1; i >= 0; i--)
  47.         {
  48.             result[i] = X[pos];
  49.             pos = P[pos];
  50.         }
  51.         return result;             
  52.     }
  53.  
  54.     /** Main Function **/
  55.     public static void main(String[] args) 
  56.     {    
  57.         Scanner scan = new Scanner(System.in);
  58.         System.out.println("Longest Increasing Subsequence Algorithm Test\n");
  59.  
  60.         System.out.println("Enter number of elements");
  61.         int n = scan.nextInt();
  62.         int[] arr = new int[n + 1];
  63.         System.out.println("\nEnter "+ n +" elements");
  64.         for (int i = 1; i <= n; i++)
  65.             arr[i] = scan.nextInt();
  66.  
  67.         LongestIncreasingSubsequence obj = new LongestIncreasingSubsequence(); 
  68.         int[] result = obj.lis(arr);       
  69.  
  70.         /** print result **/ 
  71.  
  72.         System.out.print("\nLongest Increasing Subsequence : ");
  73.         for (int i = 0; i < result.length; i++)
  74.             System.out.print(result[i] +" ");
  75.         System.out.println();
  76.     }
  77. }

Longest Increasing Subsequence Algorithm Test
 
Enter number of elements
16
 
Enter 16 elements
0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15
 
Longest Increasing Subsequence : 0 2 6 9 11 15

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.