Skip to content

Commit 6c2c6bd

Browse files
committed
added longest increasing subseq cpp
1 parent 16289bc commit 6c2c6bd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Dynamic Programming C++ implementation of LIS problem */
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int lis( int arr[], int n ) {
6+
int lis[n];
7+
8+
lis[0] = 1;
9+
10+
for (int i = 1; i < n; i++ ) {
11+
lis[i] = 1;
12+
for (int j = 0; j < i; j++ )
13+
if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
14+
lis[i] = lis[j] + 1;
15+
}
16+
17+
return *max_element(lis, lis+n);
18+
}
19+
20+
// main function
21+
int main() {
22+
int n;
23+
cout<<"enter size of array: ";
24+
cin>>n;
25+
26+
int arr[n];
27+
cout<<"enter elements: \n";
28+
for(int i=0;i<n;i++) cin>>arr[i];
29+
30+
printf("Length of lis is %d\n", lis( arr, n ) );
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)