Longest Bitonic Subsequence - Problem

A bitonic subsequence is a subsequence that first strictly increases and then strictly decreases. Given an array of integers, find the length of the longest bitonic subsequence.

A subsequence is derived from the original array by deleting some or no elements without changing the order of the remaining elements.

Note: A purely increasing or purely decreasing subsequence is also considered bitonic.

Input & Output

Example 1 — Standard Bitonic
$ Input: nums = [1,11,2,10,4,5,2,1]
Output: 6
💡 Note: The longest bitonic subsequence is [1,2,10,5,2,1] or [1,4,5,2,1]. Both have length 6 - they increase to a peak then decrease.
Example 2 — Pure Increasing
$ Input: nums = [1,2,3,4,5]
Output: 5
💡 Note: The array is strictly increasing. A purely increasing sequence is also considered bitonic, so the answer is 5.
Example 3 — Pure Decreasing
$ Input: nums = [5,4,3,2,1]
Output: 5
💡 Note: The array is strictly decreasing. A purely decreasing sequence is also considered bitonic, so the answer is 5.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -106 ≤ nums[i] ≤ 106

Visualization

Tap to expand
INPUTArray: [1,11,2,10,4,5,2,1]1112104521Need to find longestup-then-down patternALGORITHM1Compute LIS ending at each position2Compute LDS starting at each position3For each i: combine LIS[i] + LDS[i] - 14Return maximum combined lengthLIS: [1,2,2,3,3,4,3,2]LDS: [2,4,2,4,3,3,2,1]Combined: [2,5,3,6,5,6,4,2]RESULT6Maximum bitonic lengthExample subsequence:[1,2,10,5,2,1]Increases: 1→2→10Decreases: 10→5→2→1Key Insight:Every bitonic subsequence has a peak - find the longest path up to each point and down from each point.TutorialsPoint - Longest Bitonic Subsequence | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 25
28.0K Views
Medium-High Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen