
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program for Longest Increasing Subsequence
Following is the Java program for longest increasing subsequence −
Example
public class Demo{ static int incre_subseq(int my_arr[], int arr_len){ int seq_arr[] = new int[arr_len]; int i, j, max = 0; for (i = 0; i < arr_len; i++) seq_arr[i] = 1; for (i = 1; i < arr_len; i++) for (j = 0; j < i; j++) if (my_arr[i] > my_arr[j] && seq_arr[i] < seq_arr[j] + 1) seq_arr[i] = seq_arr[j] + 1; for (i = 0; i < arr_len; i++) if (max < seq_arr[i]) max = seq_arr[i]; return max; } public static void main(String args[]){ int my_arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; int arr_len = my_arr.length; System.out.println("The length of the longest increasing subsequence is " + incre_subseq(my_arr, arr_len)); } }
Output
The length of the longest increasing subsequence is 5
A class named Demo contains a static function named 'incre_subseq’ that takes the array and the length of the array as parameters. Inside this function, a new array is created that is empty. A 'max' variable is assigned the value 0. A 'for' loop iterates over the length of the array and every element is initialized to 1.
Again, 'for' loop is iterated, and another 'for' loop is initiated that checks if first element in the array is equal to the second element and if the array (seq_arr, that had all 1s initialized) has first element lesser than the second element + 1. The maximum of the elements in the seq_arr is found and returned. This is dynamic programming technique wherein one value is computed and stored in an array, removing the need to compute it again and again as in recursion. Whenever a previously computed element is required, it is fetched from the array.
- Related Articles
- Longest Increasing Subsequence
- Java Program for Longest Palindromic Subsequence
- Java Program for Longest Common Subsequence
- Longest Increasing Subsequence in Python
- Longest Continuous Increasing Subsequence in C++
- Program to find length of longest increasing subsequence in Python
- C++ Program for Longest Common Subsequence
- Number of Longest Increasing Subsequence in C++
- Program to find length of longest circular increasing subsequence in python
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Longest Common Subsequence
- Longest Bitonic Subsequence
- Longest Palindromic Subsequence
- Maximum product of an increasing subsequence in C++ Program
