Skip to main content
Back to problems
Leetcode
Medium
Arrays
Two Pointers
Sliding Window
Math
Subarray Product Less Than K

Count the number of contiguous subarrays whose product is strictly less than a given threshold.

Acceptance 86%
Problem Statement

Given an array of positive integers and a positive integer kk, count how many contiguous subarrays have a product strictly smaller than kk.

A subarray must contain consecutive elements. You need to return the total number of such subarrays.

Input Format

  • An integer array nums of positive integers.
  • An integer k.

You may assume the input is provided in the usual problem format for a coding platform.

Output Format

  • Return a single integer: the number of contiguous subarrays whose product is strictly less than k.

Constraints

  • 1 <= nums.length
  • nums[i] > 0
  • k > 0

For practice purposes, the exact platform limits are not included here.

Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [10, 5, 2, 6], k = 100

Output

8

Explanation

The valid subarrays are [10], [5], [2], [6], [10,5], [5,2], [2,6], and [5,2,6].

Example 2

Input

nums = [1, 2, 3], k = 0

Output

0

Explanation

No positive-product subarray can be strictly less than 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.