# Longest Increasing Subsequence

Given a sequence **A** of size **N**, find the **length **of the** longest increasing subsequence** from the given sequence.

The longest increasing subsequence means to find a subsequence of a given sequence where the subsequence's elements are sorted in increasing order, and the subsequence is longest possible. This subsequence is not necessarily contiguous, or unique. **Longest increasing subsequence** is strictly increasing.

Input:
N=7
Sequence:
{2, 3, 4, 0, 1, 2, 3, 8, 6, 4}
Output:
Length of Longest increasing subsequence is 5
Longest increasing subsequence= {0, 1, 2, 3, 8} or {0, 1, 2, 3, 4}

**Explanation with example**

The possible increasing sub-sequences are,

**Of Length 1 //each element itself is an increasing sequence**

So, on...

So, on...

So, on...

**No more**

Of Length 6

None

**So, the longest increasing subsequence length is 5.**

Of course, in brute-force we can simply generate all increasing sequences and find the longest one. But it would take exponential time which is not a feasible solution. Hence, we choose Dynamic programming to solve.

We create a DP table to store longest increasing subsequence length.

It's intuitive that the minimum value would be 1 as each element represents the primitive sequence which is an increasing one.

So, the base value is 1.

Now,

So,

To compute

L(i)the recursion function is,As, the base value is 1, for every index

i,L(i)is at least 1.Initially DP table,So, the maximum out of this is 5Hence, LIS=5.

C++ implementation:

need an explanation for this answer? contact us directly to get an explanation for this answerOutput