DSA Tracker

Medium

Longest Bitonic Subsequence

A medium Dynamic Programming problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Dynamic Programming
Sheets
1
Core for
9 roles
Platform
GeeksforGeeks

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 = [1,11,2,10,4,5,2,1]
Output
6
Why
One longest bitonic subsequence is [1,2,10,4,2,1] or [1,11,10,4,2,1], length 6.

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-09

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.

What each operation costs

OperationTime
fill dynamic programming table of n statesO(n)
solve two-dimensional grid of m by n statesO(m * n)
space-optimized state transition keeping one rowO(n)
What usually goes wrong with Dynamic Programming
  • Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
  • Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
  • Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.

Which roles need this problem

Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Dynamic Programming problems

Problem set and role mapping as of .