Task: Given a sequence of an array of size N, find longest increasing sub-sequence from given sequence. The sub-sequence is need not to be continuous.
Example:
Input:
1
6
5 8 3 7 9 1
Output:
3 // 5 7 9
Here is the Code:
Program in C++:
- #include<iostream>
 - using namespace std;
 - int longestIncreasingSubsequence(int *a,int n)
 - {
 - int dp[n],lis=1;
 - dp[0]=1;
 - for(int i=1;i<n;i++)
 - {
 - int j=0,t=1;
 - while(j<i)
 - {
 - if(a[i]>a[j]&&t<=dp[j])
 - {
 - t++;
 - }
 - j++;
 - }
 - dp[i]=t;
 - if(lis<dp[i]) lis=dp[i];
 - }
 - return lis;
 - }
 - int main()
 - {
 - int t;
 - cin>>t;
 - while(t--)
 - {
 - int n;
 - cin>>n;
 - int a[n];
 - for(int i=0;i<n;i++)
 - {
 - cin>>a[i];
 - }
 - cout<<longestIncreasingSubsequence(a,n)<<endl;
 - }
 - return 0;
 - }
 
Here is the Video:
Comments
Post a Comment