Finding maximum length of common subarray in JavaScript

Finding the maximum length of a common subarray (also known as the longest common subarray) is a classic dynamic programming problem. We need to find the longest contiguous sequence that appears in both arrays in the same order.

Problem Statement

We are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively. Our function should return the maximum length of a subarray that appears in both arrays.

For example, if the input arrays are:

const arr1 = [1, 2, 3, 2, 1];
const arr2 = [3, 2, 1, 4, 7];

The common subarray with maximum length is [3, 2, 1], so the output should be 3.

Algorithm Approach

We use dynamic programming to solve this problem efficiently. The approach involves creating a 2D table where dp[i][j] represents the length of the common subarray ending at arr1[i] and arr2[j].

3 2 1 4 7 1 2 3 2 1 0 0 1 0 0 0 1 0 2 0 1 0 0 0 3 DP Table for Common Subarray Red values show matching elements forming the longest subarray

Implementation

const arr1 = [1, 2, 3, 2, 1];
const arr2 = [3, 2, 1, 4, 7];

const maximumLength = (arr1 = [], arr2 = []) => {
    const dp = new Array(arr1.length + 1).fill(0).map(() => new Array(arr2.length + 1).fill(0));
    let maxLength = 0;
    
    for (let i = arr1.length - 1; i >= 0; i--) {
        for (let j = arr2.length - 1; j >= 0; j--) {
            if (arr1[i] === arr2[j]) {
                dp[i][j] = dp[i + 1][j + 1] + 1;
                maxLength = Math.max(maxLength, dp[i][j]);
            } else {
                dp[i][j] = 0;
            }
        }
    }
    
    return maxLength;
};

console.log(maximumLength(arr1, arr2));
3

How It Works

The algorithm works by comparing each element of arr1 with each element of arr2. When elements match, we extend the length from the previous matching position. The key insight is that we traverse from right to left (bottom-up approach) to build the solution incrementally.

Alternative Approach

Here's a more optimized version that tracks the maximum length directly without needing to scan the entire DP table:

const maximumLengthOptimized = (arr1 = [], arr2 = []) => {
    const dp = new Array(arr1.length + 1).fill(0).map(() => new Array(arr2.length + 1).fill(0));
    let maxLength = 0;
    
    for (let i = 1; i <= arr1.length; i++) {
        for (let j = 1; j <= arr2.length; j++) {
            if (arr1[i - 1] === arr2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
                maxLength = Math.max(maxLength, dp[i][j]);
            }
        }
    }
    
    return maxLength;
};

console.log(maximumLengthOptimized([1, 2, 3, 2, 1], [3, 2, 1, 4, 7]));
3

Time and Space Complexity

Both approaches have a time complexity of O(m × n) where m and n are the lengths of the input arrays. The space complexity is also O(m × n) for the DP table.

Conclusion

The maximum length common subarray problem is efficiently solved using dynamic programming. The key is to track contiguous matching elements and reset when elements don't match, keeping track of the maximum length encountered.

Updated on: 2026-03-15T23:19:00+05:30

413 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements