- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Product of subarray just less than target in JavaScript

## Problem

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.

For example, if the input to the function is

**Input**

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

**Output**

const output = 8;

**Output Explanation**

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 k.

## Example

Following is the code −

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] while (left <= right && product >= target) { product /= arr[left] left += 1 } count += right - left + 1 } return count }; console.log(countSubarrays(arr, target));

## Output

8

- Related Articles
- Subarray Product Less Than K in C++
- Sum of two elements just less than n in JavaScript\n
- Subarray with the greatest product in JavaScript
- Left right subarray sum product - JavaScript
- JavaScript Program for Maximum Product Subarray
- Find Smallest Letter Greater Than Target in JavaScript
- Program to find sum of two numbers which are less than the target in Python
- Count all subsequences having product less than K in C++
- Count ordered pairs with product less than N in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit in C++
- Maximum Product Subarray in Python
- Number of elements less than or equal to a given number in a given subarray in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum Product Subarray | Added negative product case in C++

Advertisements