diff --git a/Dynamic Programming/Longest Bitonic Subsequence/README.md b/Dynamic Programming/Longest Bitonic Subsequence/README.md new file mode 100644 index 00000000..bca528d1 --- /dev/null +++ b/Dynamic Programming/Longest Bitonic Subsequence/README.md @@ -0,0 +1,32 @@ +# Longest Bitonic Subsequence + +## Problem Description + +A **bitonic subsequence** of a sequence is a subsequence that first increases and then decreases. The **Longest Bitonic Subsequence (LBS)** problem is to find the length of the longest subsequence in a given sequence that is bitonic. + +In this specific implementation, we are working with a string and finding the longest bitonic subsequence by treating each character as an element in the sequence. + +### Example + +**Input:** +`str = "character"` + +**Output:** +`Length of Longest Bitonic Subsequence is 5` + +**Explanation:** +The longest bitonic subsequence here is `"charer"`, which first increases (`c -> h -> a -> r`) and then decreases (`r -> e -> r`), resulting in a length of 5. + +## Approach + +We solve this problem using **Dynamic Programming** by breaking it into two main parts: +1. **Longest Increasing Subsequence (LIS):** For each character in the sequence, calculate the length of the longest subsequence ending at that character. +2. **Longest Decreasing Subsequence (LDS):** For each character in the sequence, calculate the length of the longest subsequence starting from that character. + +For each position `i`, the length of the longest bitonic subsequence passing through `i` is `LIS[i] + LDS[i] - 1`. + +### Time Complexity +The solution has a time complexity of **O(n²)**, where `n` is the length of the input string. + +### Space Complexity +The space complexity is **O(n)** for the `inc` and `dec` arrays. \ No newline at end of file diff --git a/Dynamic Programming/Longest Bitonic Subsequence/program.c b/Dynamic Programming/Longest Bitonic Subsequence/program.c new file mode 100644 index 00000000..48a55003 --- /dev/null +++ b/Dynamic Programming/Longest Bitonic Subsequence/program.c @@ -0,0 +1,54 @@ +#include +#include + +int max(int a, int b) { + return (a > b) ? a : b; +} + +int longestBitonicSubsequence(const char* str) { + int n = strlen(str); + int inc[n]; + int dec[n]; + + // Initialize all values to 1 for the increasing subsequence + for (int i = 0; i < n; i++) { + inc[i] = 1; + } + + // Calculate longest increasing subsequence (LIS) ending at each position + for (int i = 1; i < n; i++) { + for (int j = 0; j < i; j++) { + if (str[i] > str[j]) { + inc[i] = max(inc[i], inc[j] + 1); + } + } + } + + // Initialize all values to 1 for the decreasing subsequence + for (int i = 0; i < n; i++) { + dec[i] = 1; + } + + // Calculate longest decreasing subsequence (LDS) starting at each position + for (int i = n - 2; i >= 0; i--) { + for (int j = n - 1; j > i; j--) { + if (str[i] > str[j]) { + dec[i] = max(dec[i], dec[j] + 1); + } + } + } + + // Find the maximum value of inc[i] + dec[i] - 1 + int maxLength = 0; + for (int i = 0; i < n; i++) { + maxLength = max(maxLength, inc[i] + dec[i] - 1); + } + + return maxLength; +} + +int main() { + const char* str = "character"; + printf("Length of Longest Bitonic Subsequence is %d\n", longestBitonicSubsequence(str)); + return 0; +}