diff --git a/Array/Longest_increasing_subsequence.cpp b/Array/Longest_increasing_subsequence.cpp new file mode 100644 index 00000000..bdd75912 --- /dev/null +++ b/Array/Longest_increasing_subsequence.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace std; + +int lis(vector &arr) +{ + int n = arr.size(); + vector lisLength(n, 1); + + for (int i = 1; i < n; i++) + { + for (int j = 0; j < i; j++) + { + if (arr[i] > arr[j] && lisLength[i] < lisLength[j] + 1) + { + lisLength[i] = lisLength[j] + 1; + } + } + } + + return *max_element(lisLength.begin(), lisLength.end()); +} + +int main() +{ + int n; + cin >> n; + + vector arr(n); + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + int result = lis(arr); + + cout << result << endl; + + return 0; +}