Maximum Length of Repeated Subarray

Given two integer arrays `nums1` and `nums2`, return the maximum length of a subarray that appears in both arrays.

30:00

Maximum Length of Repeated Subarray
medium
Free written solution
Topics
Companies

Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.

Example 1:
Input: {"nums1":[1,2,3,2,1],"nums2":[3,2,1,4,7]}
Output: 3
Constraints:
  • 1≤nums1.length,nums2.length≤10001 \leq \text{nums1.length}, \text{nums2.length} \leq 1000

  • 0≤nums1[i],nums2[i]≤1000 \leq \text{nums1}[i], \text{nums2}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums1":[1,2,3,2,1],"nums2":[3,2,1,4,7]}

Slide nums2 underneath nums1 one position at a time. In each alignment, a shared subarray is a run of equal pairs stacked on top of each other.

nums1 fixed on top, nums2 slides underneath

1
2
3
2
1

longest shared run so far

none yet

shift

-

run

0

best

0

Variables
VariableValue
n5
m5
best0
DepthFunction Call
Stack empty
0/35