Subarray Product Less Than K

Subarray Product Less Than K is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.

Problem statement

Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

Example 1:

Input: nums = [10,5,2,6], k = 100 Output: 8 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 2:

Input: nums = [1,2,3], k = 0 Output: 0

Constraints:

- 1 <= nums.length <= 3 104

- 1 <= nums[i] <= 1000

- 0 <= k <= 106

Topics and companies

Topics: Array, Binary Search, Sliding Window, Prefix Sum.

Reported in interviews at Bloomberg, Google, LinkedIn.

How to practise Subarray Product Less Than K on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Subarray Product Less Than K in the code editor, or read the Subarray Product Less Than K editorial for a worked solution with its approach and complexity analysis.

Where Subarray Product Less Than K sits in the DSA pattern sheet

Problems related to Subarray Product Less Than K

Other problems that use the same Variable Size (Condition-Based) and Sliding Window techniques:

Opening the problem

Progress is saved as you study.