-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Increasing Sequence.cpp
More file actions
48 lines (48 loc) · 1.15 KB
/
Longest Increasing Sequence.cpp
File metadata and controls
48 lines (48 loc) · 1.15 KB
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
#include <iostream>
#include <vector>
#define int long long int
using namespace std;
// Time Complexity is O(n^2) And Space Complexity is O(n).
int LIS(const int *ar,int n,int *lis){
for (int i = 0; i < n; ++i) {
lis[i] = 1;
}
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (ar[i]>ar[j] && lis[i]<lis[j]+1){
lis[i]= lis[j] + 1;
}
}
}
int result = 0;
for (int i = 0; i < n; ++i) {
result = max(result,lis[i]);
}
return result;
}
int32_t main(){
cout<<"Enter The value of n:\n";
int n;
cin>>n;
cout<<"Enter The value of array:\n";
int *ar = new int[n];
for (int i = 0; i < n; ++i) {
cin>>ar[i];
}
int *lis = new int[n];
int res = LIS(ar,n,lis);
cout<<"Longest Increasing Sequence is : "<<res<<"\n";
int prev= INT_MAX;
vector<int> v;
for (int i = n-1; i >=0; i--) {
if (ar[i]<=prev && lis[i]==res){
v.push_back(ar[i]);
res--;
prev = ar[i];
}
}
cout<<"Sequence is :\n";
for (int i = v.size()-1; i >=0 ; --i) {
cout<<v[i]<<" ";
}
}