diff --git a/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence Readme.md b/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence Readme.md new file mode 100644 index 000000000..5d1e7062a --- /dev/null +++ b/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence Readme.md @@ -0,0 +1,28 @@ +Problem :- +======= +To find the longest Bitonic Subsequence(a bitonic subsequence is a sequence which strictly increases first then strictly decrases) in a given array of numbers. + +Input :- +----- +The size of an array(n) and the contents of the array(arr). + +Output :- +------ +A single integer printing the maximum length of the bitonic subsequence present in the given array. + +#### Language : `C++` + +#### Algorithm Paradigm : `Dynamic Programming` + +#### Time Complexity : `O(N^2)` + +#### Space Complexity : `O(N)` + +Working :- +------- +This problem is an application of the longest increasing subsequence problem also solved using dynamic programming. + +For any `arr[i]` we are finding the longest increasing subsequence from left to right keeping the `arr[i]` the last element(lets call it `a`).Then we are finding the longest increasing subsequence from right to left keeping `arr[i]` the last element(lets call it `b`).Then we are finding `a+b-1` (subtracting `1` because `arr[i]` has been counted twice) which gives us the length of the bitonic subsequence with `arr[i]` as its peak element. +We are repeating above process for all the elements in the array and finding the max value of all `a+b-1` which gives us the length of the longest bitonic subsequence. + +By using dynamic programming we are bringing the time complexity down from exponential(as in case of brute force solution) to polynomial. \ No newline at end of file diff --git a/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence.cpp b/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence.cpp new file mode 100644 index 000000000..5943aebf2 --- /dev/null +++ b/Competitive Coding/Dynamic Programming/Longest Bitonic Subsequence/Longest Bitonic Subsequence.cpp @@ -0,0 +1,63 @@ +//C++ program to find the longest bitonic sequence. +//A bitonic sequence is a sequence which increases first then decreases. +#include + +using namespace std; + +int main() +{ + int n;//n would store the size of the array. + cout<<"Enter the length of the array : "; + cin>>n;//Inputting the size of the array. + cout<>arr[i];//Inputting the values in the array. + } + + /*temp1[i] would store the longest increasing subsequence from left to right if arr[i] was the last element of the + subsequene.*/ + int temp1[n]; + for(int i=0;iarr[j]) + temp1[i]=max(temp1[i],temp1[j]+1); + } + } + + /*temp2[i] would store the longest increasing subsequence from right to left if arr[i] was the last element of the + subsequence*/ + int temp2[n]; + for(int i=0;i=0;i--)//Implementing the algorithm to find temp2[i]. + { + for(int j=n-1;j>i;j--) + { + if(arr[i]>arr[j]) + temp2[i]=max(temp2[i],temp2[j]+1); + } + } + + int max=-1;//max would store the maximum length of the longest bitonic subsequence. + for(int i=0;i