Longest Increasing Subsequence(N log N)

#include<bits/stdc++.h>
using namespace std;
const int inf = 2e9;
int lis(int s[], int n)
{
    int I[n+1], i;
    I[0] = -inf;

    for(i=1;i<=n;i++)
        I[i] = inf;

    int total_size = 0;

    for(i=0;i<n;i++)
    {
        int low = 0, high = total_size, mid = 0;

        while(low <= high)
        {
            mid = (low+high)/2;
            if(I[mid] < s[i])
                low = mid + 1;
            else
                high = mid - 1;
        }
        I[low] = s[i];
        if(total_size < low)
            total_size = low;
    }

    return total_size;
}
int main()
{
    int n, i;
    cout << "Enter Array Size :: ";
    cin >> n;

    int s[n];

    cout << "\nEnter Array Element :: ";
    for(i=0;i<n;i++)
        cin >> s[i];

    int length = lis(s, n);

    cout << "\nLongest Increasing Subsequence Length is :: " << length << "\n";

    return 0;
}


Comments