Product of subarray just less than target in JavaScript

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument.

Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target.

Problem Example

For example, if the input to the function is:

const arr = [10, 5, 2, 6];
const target = 100;

The expected output is:

8

The 8 subarrays that have product less than 100 are:

[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]

Note that [10, 5, 2] is not included as the product of 100 is not strictly less than the target.

Algorithm Approach

We use the sliding window technique with two pointers:

  • Right pointer: Expands the window by including new elements
  • Left pointer: Shrinks the window when product exceeds target
  • Count logic: For each valid window ending at right, we add (right - left + 1) subarrays

Implementation

const arr = [10, 5, 2, 6];
const target = 100;

const countSubarrays = (arr = [], target = 1) => {
    let product = 1;
    let left = 0;
    let count = 0;
    
    for (let right = 0; right < arr.length; right++) {
        product *= arr[right];
        
        // Shrink window while product >= target
        while (left <= right && product >= target) {
            product /= arr[left];
            left += 1;
        }
        
        // Add count of subarrays ending at 'right'
        count += right - left + 1;
    }
    
    return count;
};

console.log(countSubarrays(arr, target));
8

How It Works

Let's trace through the algorithm step by step:

const traceAlgorithm = (arr, target) => {
    let product = 1, left = 0, count = 0;
    
    for (let right = 0; right < arr.length; right++) {
        product *= arr[right];
        console.log(`Step ${right + 1}: Added arr[${right}] = ${arr[right]}, product = ${product}`);
        
        while (left <= right && product >= target) {
            product /= arr[left];
            console.log(`  Removed arr[${left}] = ${arr[left]}, product = ${product}`);
            left += 1;
        }
        
        const newSubarrays = right - left + 1;
        count += newSubarrays;
        console.log(`  Valid subarrays ending at index ${right}: ${newSubarrays}, total count: ${count}<br>`);
    }
    
    return count;
};

traceAlgorithm([10, 5, 2, 6], 100);
Step 1: Added arr[0] = 10, product = 10
  Valid subarrays ending at index 0: 1, total count: 1

Step 2: Added arr[1] = 5, product = 50
  Valid subarrays ending at index 1: 2, total count: 3

Step 3: Added arr[2] = 2, product = 100
  Removed arr[0] = 10, product = 10
  Valid subarrays ending at index 2: 2, total count: 5

Step 4: Added arr[3] = 6, product = 60
  Valid subarrays ending at index 3: 3, total count: 8

Time and Space Complexity

Aspect Complexity Explanation
Time O(n) Each element is visited at most twice
Space O(1) Only constant extra space used

Conclusion

The sliding window technique efficiently solves this problem in linear time by maintaining a valid window where the product is less than the target. For each position, we count all possible subarrays ending at that position.

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

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements