DSA Tracker

Problem

Medium

Longest Bitonic Subsequence

A medium Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Dynamic Programming
Sheets
1
Core for
9 roles

The problem

Given an array of integers, find the length of the longest bitonic subsequence. A bitonic sequence first increases then decreases.

Example 1

Input
nums = [4,7,2,8,3,9,1,5]
Output
5
Why
One longest bitonic subsequence: [2,8,9,5,1] or [4,7,8,9,5] - length 5.

Example 2

Input
nums = [12,11,40,5,3,1]
Output
5
Why
The longest bitonic subsequence is [12,11,5,3,1] or [12,40,5,3,1], length 5.

Example 3

Input
nums = [80,60,30,40,20,10]
Output
5
Why
The longest bitonic subsequence is [80,60,40,20,10] or [80,30,40,20,10], length 5.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^5

How to think about it

Updated 2026-09-29

Every bitonic sequence reaches an apex element where it stops increasing and starts decreasing. Compute the longest increasing subsequence ending at each index from the left, and the longest decreasing subsequence starting at each index toward the right. At each candidate apex i, sum the two lengths and subtract 1 for the shared peak.

Approaches, worst first

  1. Peak-by-peak subsequence search

    time O(n^3) · space O(n)

    Iterate over every possible peak index i, finding the longest increasing subsequence in nums[0..i] and decreasing subsequence in nums[i..n-1] independently. Inefficient due to duplicated work.

  2. Bidirectional dynamic programming sweeps

    time O(n^2) · space O(n)

    Compute array lis where lis[i] is LIS ending at i by scanning left-to-right. Compute lds where lds[i] is LDS starting at i by scanning right-to-left. Maximize lis[i] + lds[i] - 1 across all i.

  3. Binary search patience sorting sweepsWrite this one

    time O(n log n) · space O(n)

    Run patience sorting twice: forward to compute lis lengths ending at each position, and backward to compute lds lengths starting at each position. Combine the arrays in a final linear pass.

Where people lose marks · 3
  • Double-counting the peak element: both lis[i] and lds[i] include nums[i], so the combined length must subtract 1.
  • Handling strictly monotonic sequences: depending on the problem variant, a bitonic sequence may require both an increasing phase and a decreasing phase (lis > 1 and lds > 1), or allow flat one-sided runs.
  • Off-by-one errors when sweeping the reverse LDS pass from right to left.

The theory behind it

Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems

What Dynamic Programming is

Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.

When to reach for it

Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.

How the pattern works

Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.

More Dynamic Programming problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .