Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Strings
Amazon
Maximum Length of Repeated Subarray

Find the length of the longest subarray that appears in both input arrays.

Acceptance 0%
Problem Statement

Problem

Given two integer arrays, return the length of the longest contiguous segment that appears in both arrays.

A repeated subarray must be contiguous in each array. You are looking for the maximum possible length of such a shared segment.

Input Format

  • Two integer arrays nums1 and nums2.

Output Format

  • Return one integer: the maximum length of a common contiguous subarray.

Constraints

  • The arrays may be empty.
  • Values may repeat.
  • The matching segment must be contiguous in both arrays.

Notes

  • A subarray is a contiguous slice of an array.
  • The answer is 0 if no common subarray exists.

Input Format

  • Two integer arrays nums1 and nums2.

Output Format

  • Return the maximum length of a contiguous subarray appearing in both arrays.

Constraints

  • The matching segment must be contiguous in both arrays.
  • Arrays may be empty.
  • Duplicate values are allowed.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

3

Explanation

The longest common contiguous subarray is [3,2,1], which has length 3.

Example 2

Input

nums1 = [0,0,0,0,1]
nums2 = [0,0,0,1,0]

Output

4

Explanation

The longest common contiguous subarray is [0,0,0,1], which has length 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.