Find Longest Increasing Subsequence
💻 coding

Find Longest Increasing Subsequence

1 min read 149 words
1 min read
ShareWhatsAppPost on X
  • 1The Longest Increasing Subsequence (LIS) problem involves finding the length of a subsequence in an array sorted in strictly ascending order.
  • 2The provided algorithm operates with a time complexity of O(N^2) to determine the LIS length from a given array of integers.
  • 3The implementation initializes a result array and iteratively updates it to track the maximum length of increasing subsequences.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The Longest Increasing Subsequence (LIS) problem involves finding the length of a subsequence in an array sorted in strictly ascending order."

Find Longest Increasing Subsequence

Finding Longest Increasing Subsequence in an array in N^2 Time complexity.

The task is to find the length of the longest subsequence in a given array of integers such that all elements of the subsequence are sorted in strictly ascending order. This is called the Longest Increasing Subsequence (LIS) problem.

public class LongestIncreasingSubsequence {

	private static int Max(int a, int b) {
		return a>b?a:b;
	}
	
	public static void main(String[] args) {
		
		int[] arr = new int[] {5, 2, 7, 4, 3, 8};
		//Array to store the result
		int[] resArr = new int[arr.length];
		
		//Initialize every element of resArr to 1
		for(int i=0;i<arr.length;i++) {
			resArr[i]=1;
		}
		
		//Calculate each element value for resArr
		for(int i=0;i<arr.length-1;i++)
		{
			for(int j=1;j<arr.length;j++)
			{
				if(arr[i]<arr[j]) {
					resArr[j] = Max(resArr[j],resArr[i]+1);
				}
			}
		}
		
		//Find the maximum element in resArr Array
		int max = 0;
		for(int i=0;i<arr.length;i++) {
			if(resArr[i]>max)
				max = resArr[i];
		}
		
		//Print the result
		System.out.println(max);
	}

}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 12 July 2018 · 1 min read · 149 words

Part of AskGif Blog · coding

You might also like