2Brute2Best
🔥0

Maximum Length of Repeated Subarray

Medium·LeetCode #718 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get two arrays of integers, nums1 and nums2. Return the length of the longest subarray that appears in both arrays.

A subarray is a run of values that sit next to each other. A gap is not allowed.

Input

Two integer arrays nums1 and nums2.

Output

The length of the longest subarray that appears in both arrays.

Example 1
Input: nums1 = [1, 2, 3, 2, 1], nums2 = [3, 2, 1, 4, 7]
Output: 3

The repeated subarray is [3, 2, 1]. It sits at positions 2–4 in nums1 and at positions 0–2 in nums2.

Example 2
Input: nums1 = [0, 0, 0, 0, 0], nums2 = [0, 0, 0, 0, 0]
Output: 5

All five zeros match, so the whole array is the repeated subarray.

Constraints
  • • 1 ≤ nums1.length, nums2.length ≤ 1000
  • • 0 ≤ nums1[i], nums2[i] ≤ 100
02

Pattern Recognition Clues

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

“subarray”
→ A run of values that sit next to each other. A gap means the run starts again from 0.
“appears in both arrays”
→ The run must match in nums1 and in nums2 at the same time, so we compare pairs of positions, one from each array.
“maximum length”
→ The answer is the longest run that ends at any pair, not the value at the last pair.
03

Approach Complexity Ladder

Brute Force — start at every pair, extend while equalO(N · M · min(N, M))O(1)

Try every pair of start positions and extend the run while the values match

Better — 2-D table, dp[i][j] = subarray ending at this pairO(N · M)O(N · M)

Fill an (N + 1) × (M + 1) table; a match extends the diagonal cell by one

Optimal — one row, j goes from high to lowO(N · M)O(M)

Keep one row; on a match row[j] = row[j − 1] + 1, on a mismatch 0; read j from high to low

Brute Force Intuition

Pick a start in nums1 and a start in nums2. Walk both forward together while the values match, and record how far they go. Do this for every pair of starts and keep the longest run. Each walk starts from scratch, so the same values are compared again and again.

Recognition rule for this sub-pattern:

Substring must be contiguous: on mismatch the run resets to 0.
Remember this · DP — Strings
Trigger
Longest common subsequence, edit distance, palindromes, matching with wildcards, interleaving.
Think
Look at the last characters: if they match, what do I do? If not, what are my options?
Key question
What does dp[i][j] mean, and what are the empty-string base cases?
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 — Strings translate into production architectures and low-level memory mechanics.