forked from profpiyush/Hacktberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest common subsequence
54 lines (48 loc) · 1.01 KB
/
longest common subsequence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution
{
public:
//Function to find length of longest increasing subsequence.
int longestSubsequence(int n, int a[])
{
int *o=new int[n];
o[0]=1;
for(int i=1;i<n;i++)
{o[i]=1;
for(int j=i-1;j>=0;j--)
{
if(a[j]>=a[i])
{ continue;}
int possibleanswer=o[j]+1;
if(possibleanswer>o[i])
o[i]=possibleanswer;
}
}
int best=0;
for(int i=0;i<n;i++)
{
if(best<o[i])
best=o[i];}
return best;
// your code here
}
};
// { Driver Code Starts.
int main()
{
//taking total testcases
int t,n;
cin>>t;
while(t--)
{
//taking size of array
cin>>n;
int a[n];
//inserting elements to the array
for(int i=0;i<n;i++)
cin>>a[i];
Solution ob;
//calling method longestSubsequence()
cout << ob.longestSubsequence(n, a) << endl;
}
}
// } Driver