LeetCode Problem

How to Solve Maximum Length of Repeated Subarray

This problem involves finding the maximum length of a subarray that appears in both arrays. The solution uses dynamic programming, where dp[i][j] represents the longest common prefix of subarrays starting at indices i and j of nums1 and nums2 respectively.

GhostInterview Help

Need help with Maximum Length of Repeated Subarray without spending extra time grinding it?

GhostInterview can read Maximum Length of Repeated Subarray from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #718State transition dynamic programmingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem involves finding the maximum length of a subarray that appears in both arrays. The solution uses dynamic programming, where dp[i][j] represents the longest common prefix of subarrays starting at indices i and j of nums1 and nums2 respectively.

Problem Statement

Given two integer arrays nums1 and nums2, your task is to find the maximum length of a subarray that appears in both arrays. A subarray is a contiguous part of an array.

For example, consider nums1 = [1,2,3,2,1] and nums2 = [3,2,1,4,7]. The longest repeated subarray is [3,2,1], which has length 3. Your solution should efficiently solve this problem by leveraging dynamic programming and optimization techniques.

Examples

Example 1

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

Output: 3

The repeated subarray with maximum length is [3,2,1].

Example 2

Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]

Output: 5

The repeated subarray with maximum length is [0,0,0,0,0].

Constraints

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 100

Solution Approach

State Transition Dynamic Programming

We use dynamic programming to track the longest common subarray. Let dp[i][j] represent the length of the longest common subarray ending at nums1[i] and nums2[j]. If nums1[i] equals nums2[j], then dp[i][j] is dp[i-1][j-1] + 1; otherwise, dp[i][j] is 0.

Binary Search Optimization

We can optimize the solution using binary search to find the maximum subarray length efficiently. This reduces the time complexity by narrowing down the search space for the longest common subarray.

Space Optimization with Rolling Hash

Using a rolling hash technique can help reduce the space complexity by storing only necessary values. This allows us to avoid storing the entire dp table and thus makes the algorithm more memory efficient.

Complexity Analysis

MetricValue
TimeO((M+N) * \log{(\min(M, N))})
SpaceO(M)

The time complexity is O((M+N) * log(min(M, N))) due to binary search and dynamic programming operations. The space complexity is O(M), where M is the length of nums1, because we optimize the space by using only the current row of the dp table.

What Interviewers Usually Probe

  • Look for an understanding of state transition dynamic programming.
  • The candidate should propose optimizations such as binary search or rolling hash.
  • Pay attention to how well they can balance time and space complexity in their approach.

Common Pitfalls or Variants

Common pitfalls

  • Incorrectly handling indices while accessing elements in the dp table.
  • Over-complicating the solution when a simple dynamic programming approach suffices.
  • Failing to optimize for space when the problem constraints are large.

Follow-up variants

  • What if the arrays are very large (greater than 1000 elements)?
  • Can this problem be solved in linear time?
  • What if the arrays contain non-integer values or negative numbers?

How GhostInterview Helps

  • GhostInterview will guide you through the state transition dynamic programming approach, ensuring you understand how to track subarray matches.
  • You'll learn how to apply binary search to reduce time complexity, improving your solution's efficiency.
  • You'll get step-by-step hints on optimizing space usage by leveraging rolling hash techniques.

Topic Pages

FAQ

What is the primary approach to solving the Maximum Length of Repeated Subarray problem?

The problem is typically solved using state transition dynamic programming, where dp[i][j] represents the longest common prefix of subarrays starting at nums1[i:] and nums2[j:].

How does dynamic programming help solve the problem?

Dynamic programming is used to efficiently compute the longest common subarray by storing results of overlapping subproblems, reducing redundant calculations.

Can binary search optimize the Maximum Length of Repeated Subarray solution?

Yes, binary search can be used to narrow down the length of the longest common subarray, improving the time complexity of the solution.

What is the time complexity of the Maximum Length of Repeated Subarray problem?

The time complexity is O((M+N) * log(min(M, N))) due to the binary search and dynamic programming steps.

How can space complexity be optimized in this problem?

Space complexity can be optimized by using a rolling hash or by keeping only the current row of the dp table, thus reducing the memory usage.

GhostInterview Solver

Need direct help with Maximum Length of Repeated Subarray instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Length of Repeated Subarray from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.