Given an array of random numbers, find the longest monotonically increasing subsequence (LIS) in the array.
If you want to understand the O(NlogN) approach, it’s explained very clearly here.
In this post, a simple and time-saving implementation of O(NlogN) approach using stl is discussed. Below is the code for LIS O(NlogN) :
C
// A simple C++ implementation to find LIS#include<iostream>#include<algorithm>#include<set>using namespace std;// Return length of LIS in arr[] of size Nint lis(int arr[], int N){ int i; set<int> s; set<int>::iterator k; for (i=0; i<N; i++) { // Check if the element was actually inserted // An element in set is not inserted if it is // already present. Please see if (s.insert(arr[i]).second) { // Find the position of inserted element in iterator k k = s.find(arr[i]); k++; // Find the next greater element in set // If the new element is not inserted at the end, then // remove the greater element next to it (This is tricky) if (k!=s.end()) s.erase(k); } } // Note that set s may not contain actual LIS, but its size gives // us the length of LIS return s.size();}int main(){ int arr[] = {8, 9, 12, 10, 11}; int n = sizeof(arr)/sizeof(arr[0]); cout << lis(arr, n)<< endl;} |
chevron_right
filter_none
Java
// Java implementation // to find LISimport java.util.*;class GFG{// Return length of LIS // in arr[] of size Nstatic int lis(int arr[], int N){ int i; HashSet<Integer> s = new HashSet<>(); for (i = 0; i < N; i++) { // Check if the element // was actually inserted // An element in set is // not inserted if it is // already present. Please see // org/set-insert-function-in-c-stl/ int k = 0; int size = s.size(); if (s.add(arr[i])) { // Find the position of // inserted element in iterator k if(s.contains(arr[i])) k++; // Find the next // greater element in set // If the new element is not // inserted at the end, then // remove the greater element // next to it. if (size == s.size()) s.remove(k + 1); } } // Note that set s may not contain // actual LIS, but its size gives // us the length of LIS return s.size();}public static void main(String[] args){ int arr[] = {8, 9, 12, 10, 11}; int n = arr.length; System.out.print(lis(arr, n) + "\n");}}// This code is contributed by gauravrajput1 |
chevron_right
filter_none
Output:
4
This article is contributed by Raj Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Longest Increasing Subsequence Size (N log N)
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Construction of Longest Increasing Subsequence (N log N)
- Longest Bitonic Subsequence in O(n log n)
- Longest Increasing Subsequence | DP-3
- Longest Common Increasing Subsequence (LCS + LIS)
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- Find the Longest Increasing Subsequence in Circular manner
- C/C++ Program for Longest Increasing Subsequence
- C++ Program for Longest Increasing Subsequence
- Java Program for Longest Increasing Subsequence
- Python program for Longest Increasing Subsequence
- Longest Increasing Odd Even Subsequence
- Longest Increasing consecutive subsequence
- Printing longest Increasing consecutive subsequence
- Longest Increasing Subsequence using BIT
- Length of longest increasing index dividing subsequence
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
- Longest Increasing Subsequence having sum value atmost K
- Longest increasing subsequence which forms a subarray in the sorted representation of the array

