diff --git a/Dynamic Programming/Longest Increasing Subsequence (nlogn).cpp b/Dynamic Programming/Longest Increasing Subsequence (nlogn).cpp new file mode 100644 index 000000000..eaf28ff8c --- /dev/null +++ b/Dynamic Programming/Longest Increasing Subsequence (nlogn).cpp @@ -0,0 +1,39 @@ +//Program to calculate length of longest increasing subsequence in an array +// in O(n log n) +// tested on : https://cses.fi/problemset/task/1145/ + +#include +using namespace std; +int LIS(int arr[], int n) +{ + set active; // The current built LIS. + active.insert(arr[0]); + // Loop through every element. + for(int i=1; i arr[i] ){ + // else we remove the bigger element and add a smaller element (which is arr[i]) and continue; + active.erase(get); + active.insert(arr[i]); + } + } + } + return active.size(); // size of the LIS. +} +int main(int argc, char const *argv[]) +{ + int n; + cout << "Enter size of array: "; + cin >> n; + int a[n]; + cout << "Enter array elements: "; + for (int i = 0; i < n; ++i) + { + cin >> a[i]; + } + cout << LIS(a, n) << endl; + return 0; +}