> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/longest-bitonic-subsequence.md).

# Longest Bitonic Subsequence

Given an array arr\[0 … n-1] containing n positive integers, a [subsequence ](http://en.wikipedia.org/wiki/Subsequence)of arr\[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence.\
A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty.

**Examples:**

```
Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1};
Output: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1)

Input arr[] = {12, 11, 40, 5, 3, 1}
Output: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1)

Input arr[] = {80, 60, 30, 40, 20, 10}
Output: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10)
```

```java
class Solution {
    public int lengthOfBitonic(int[] nums) {
        if (nums.length == 0)
            return 0;
        int dpForward[] = new int[nums.length];
        int dpBackward[] = new int[nums.length];
        dpForward[0] = 1;
        dpBackward[nums.length - 1] = 1;
        for (int i = 1; i < nums.length; i++) {
            dpForward[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i])
                    dpForward[i] = Math.max(dpForward[i], dpForward[j] + 1);
            }
        }
        for (int i = nums.length - 2; i >= 0; i--) {
            dpBackward[i] = 1;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j])
                    dpBackward[i] = Math.max(dpBackward[i], dpBackward[j] + 1);
            }
        }
        int max = 0;
        for (int i = 0; i < nums.length; i++)
            max = Math.max(max, dpForward[i] + dpBackward[i] - 1);
        return max;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/longest-bitonic-subsequence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
