Longest Bitonic Subsequence

Dynamic Programming LIS Easy

Given an array arr of n integers, the task is to find the length of the longest bitonic sequence. A sequence is considered bitonic if it first increases, then decreases. The sequence does not have to be contiguous.

Examples:

Input: arr = [5, 1, 4, 2, 3, 6, 8, 7]

Output: 6

Explanation: The longest bitonic sequence is [1, 2, 3, 6, 8, 7] with length 6.

Input: arr = [10, 20, 30, 40, 50, 40, 30, 20]

Output: 8

Explanation: The entire array is bitonic, increasing up to 50 and then decreasing.

Input: arr = [12, 11, 10, 15, 18, 1 7, 16, 14]

Constraints

  • 1 <= arr.length <= 103
  • -106<= arr[i] <= 106

Hints

  • Instead of just tracking the increasing part, we also track the Longest Decreasing Subsequence (LDS) from each point.
  • "Compute LIS[i] for every i → The length of the longest increasing subsequence ending at i. Compute LDS[i] for every i → The length of the longest decreasing subsequence starting at i. Find the maximum value of LIS[i] + LDS[i] - 1, since i is counted twice."