Largest product of n contiguous digits of a number in JavaScript

We are required to write a JavaScript function that takes in two numbers as first and the second argument, let us call them m and n.

The first number will generally be a number with multiple digits and the second number will always be smaller than the number of digits in the first number.

The function should find the group of n consecutive digits from m whose product is the greatest.

For example, if the input numbers are:

const m = 65467586;
const n = 3;

Then the output should be:

280

because 7 × 5 × 8 = 280 and it's the maximum consecutive three-digit product in this number.

Algorithm

The solution uses a sliding window approach:

  • Convert the number to a string to access individual digits
  • Calculate the product of the first n digits
  • Slide the window by dividing by the leftmost digit and multiplying by the new rightmost digit
  • Keep track of the maximum product found

Example

const m = 65467586;
const n = 3;

const largestProductOfContinuousDigits = (m, n) => {
    const str = String(m);
    if(n > str.length){
        return 0;
    }
    
    let max = -Infinity;
    let temp = 1;
    
    // Calculate product of first n digits
    for(let i = 0; i < n; i++){
        temp *= +(str[i]);
    }
    max = temp;
    
    // Slide the window and update product
    for(let i = 0; i < str.length - n; i++){
        temp = (temp / (+str[i])) * (+str[i + n]);
        max = Math.max(temp, max);
    }
    
    return max;
}

console.log(largestProductOfContinuousDigits(m, n));

Output

280

Step-by-Step Breakdown

Let's trace through the algorithm with m = 65467586 and n = 3:

const traceAlgorithm = (m, n) => {
    const str = String(m);
    console.log(`Number: ${str}, Finding ${n} consecutive digits`);
    
    let max = -Infinity;
    let temp = 1;
    
    // First window: digits 0 to n-1
    for(let i = 0; i < n; i++){
        temp *= +(str[i]);
    }
    console.log(`First window (${str.substring(0, n)}): ${temp}`);
    max = temp;
    
    // Slide the window
    for(let i = 0; i < str.length - n; i++){
        const oldDigit = +str[i];
        const newDigit = +str[i + n];
        temp = (temp / oldDigit) * newDigit;
        console.log(`Window (${str.substring(i + 1, i + n + 1)}): ${temp}`);
        max = Math.max(temp, max);
    }
    
    console.log(`Maximum product: ${max}`);
    return max;
}

traceAlgorithm(65467586, 3);

Output

Number: 65467586, Finding 3 consecutive digits
First window (654): 120
Window (546): 120
Window (467): 168
Window (675): 210
Window (758): 280
Window (586): 240
Maximum product: 280

Edge Cases

const testEdgeCases = () => {
    console.log("Edge case 1 - n greater than number length:");
    console.log(largestProductOfContinuousDigits(123, 5)); // 0
    
    console.log("\nEdge case 2 - number with zeros:");
    console.log(largestProductOfContinuousDigits(1203045, 3)); // 60 (3*4*5)
    
    console.log("\nEdge case 3 - single digit:");
    console.log(largestProductOfContinuousDigits(7, 1)); // 7
}

const largestProductOfContinuousDigits = (m, n) => {
    const str = String(m);
    if(n > str.length){
        return 0;
    }
    
    let max = -Infinity;
    let temp = 1;
    
    for(let i = 0; i < n; i++){
        temp *= +(str[i]);
    }
    max = temp;
    
    for(let i = 0; i < str.length - n; i++){
        temp = (temp / (+str[i])) * (+str[i + n]);
        max = Math.max(temp, max);
    }
    
    return max;
}

testEdgeCases();

Output

Edge case 1 - n greater than number length:
0

Edge case 2 - number with zeros:
60

Edge case 3 - single digit:
7

Time Complexity

The algorithm has O(d) time complexity where d is the number of digits in m. The sliding window technique avoids recalculating the entire product for each position, making it efficient.

Conclusion

This sliding window approach efficiently finds the largest product of n consecutive digits by maintaining a running product and updating it incrementally. The solution handles edge cases and provides optimal O(n) performance.

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

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements