2Brute2Best
🔥0

Longest Common Increasing Subsequence

Hard·GeeksforGeeks Pro OnlyInterview-Level
GeeksforGeeks
01

Problem Statement

You get two arrays, a and b. Find the length of the longest sequence that appears in both arrays as a subsequence, and whose values strictly increase.

A subsequence keeps the order of the values but may skip some of them. Return the length as an int from LCIS(a, b). If the arrays share no value, return 0.

Input

Two arrays a and b. Their sizes are between 1 and 1000, and their values are whole numbers.

Output

The length of the longest common increasing subsequence.

Example 1
Input: a = [3, 4, 9, 1], b = [5, 3, 8, 9, 10, 2, 1]
Output: 2

The longest increasing subsequence that is common is [3, 9], and its length is 2.

Example 2
Input: a = [1, 1, 4, 3], b = [1, 1, 3, 4]
Output: 2

[1, 1] is common, but it is not strictly increasing. The common increasing ones are [1, 4] and [1, 3], each of length 2.

Example 3
Input: a = [1], b = [2]
Output: 0

No value is shared, so there is no common subsequence to count.

Constraints
  • • 1 ≤ a.size(), b.size() ≤ 10^3
  • • 1 ≤ a[i], b[i] ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“common subsequence of both arrays”
→ A chain must be matched in `a` and in `b`, in order. The state needs a position in `b`, not just a value.
“strictly increases”
→ Equal values cannot follow each other, so the test is `<`, not `<=`.
“longest”
→ Keep a length for each cell of `b`, and take the maximum at the end.
03

Approach Complexity Ladder

Brute Force — try every subsequence of aO(2^N · (N + M))O(N)

Try every subsequence of a; keep the ones that strictly increase and also appear in b

Better — scan every earlier cell of bO(N · M^2)O(M)

For each matching cell, scan all earlier cells of b for the best chain to extend

Optimal — one pass over b for each value of aO(N · M)O(M)

Walk b once per value of a, keeping the best dp value seen so far

Brute Force Intuition

Each subsequence of a is a choice of which values to take (2^N choices). For each one, check two things: it strictly increases, and it is a subsequence of b. Keep the longest one that passes both checks.

Recognition rule for this sub-pattern:

Same idea with a custom "can follow" rule; keep parent pointers to rebuild the chain.
Remember this · DP — Longest Increasing Subsequence
Trigger
Longest chain where each item must be bigger/fit inside the previous one (numbers, pairs, envelopes, words).
Think
Does each element extend the best chain ending at some smaller earlier element?
Key question
Sort first? Then what does "increasing" mean for these items?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.