Maximum Length of Repeated Subarray
MEDIUMDescription
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 Explanation: 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 Explanation: The repeated subarray with maximum length is [0,0,0,0,0].
Constraints:
1 <= nums1.length, nums2.length <= 10000 <= nums1[i], nums2[i] <= 100
Approaches
Checkout 4 different approaches to solve Maximum Length of Repeated Subarray. Click on different approaches to view the approach and algorithm in detail.
Brute Force
This approach exhaustively checks every possible subarray from the first array (nums1) and sees if it exists in the second array (nums2). While simple to understand, it's highly inefficient.
Algorithm
- Initialize
maxLength = 0. - Iterate through
nums1with indexifrom0tonums1.length - 1. - Inside this loop, iterate through
nums2with indexjfrom0tonums2.length - 1. - If
nums1[i] == nums2[j], it marks a potential start of a common subarray. - Start a third loop with index
kto find the length of this common subarray. - While
i + k < nums1.length,j + k < nums2.length, andnums1[i + k] == nums2[j + k], incrementk. - After the inner loop,
kis the length of the common subarray starting at(i, j). UpdatemaxLength = max(maxLength, k). - After all loops complete, return
maxLength.
We use three nested loops. The first two loops define all possible starting points i in nums1 and j in nums2. The third loop (or a while loop) then checks for the length of the common subarray starting at these points. We iterate through all pairs of starting indices (i, j) from nums1 and nums2. For each pair, we count how many consecutive elements are identical (nums1[i+k] == nums2[j+k]). We keep track of the maximum count found across all pairs.
class Solution {
public int findLength(int[] nums1, int[] nums2) {
int maxLength = 0;
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
int k = 0;
while (i + k < nums1.length && j + k < nums2.length && nums1[i + k] == nums2[j + k]) {
k++;
}
maxLength = Math.max(maxLength, k);
}
}
return maxLength;
}
}
Complexity Analysis
Pros and Cons
- Simple to conceive and implement.
- Very low memory usage.
- Extremely slow for large inputs, likely to result in a 'Time Limit Exceeded' error on most platforms.
Code Solutions
Checking out 4 solutions in different languages for Maximum Length of Repeated Subarray. Click on different languages to view the code.
class Solution {
public
int findLength(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
int[][] f = new int[m + 1][n + 1];
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (nums1[i - 1] == nums2[j - 1]) {
f[i][j] = f[i - 1][j - 1] + 1;
ans = Math.max(ans, f[i][j]);
}
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Maximum Length of Repeated Subarray
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.