Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Solution Problem Challenge 5: Counting Subarrays with Product Less than a Target

Problem Statement

Given an array nums with positive numbers and a positive integer target, return the count of contiguous subarrays whose product is less than the target number.

Examples

Example 1:

  • Input: nums = [2, 5, 3, 10], target=30
  • Output: 6
  • Explanation: There are six contiguous subarrays ([2], [5], [2, 5], [3], [5, 3], [10]) whose product is less than the target.

.....

.....

.....

Like the course? Get enrolled and start learning!
Aiman Najjar

Aiman Najjar

· 9 months ago

The algorithm description says: "While product is greater than or equal to target and left is less than or equal to right:"

However, the actual implementation actually has this condition in the while loop left < len(nums) .

kat h

kat h

· 2 years ago

Since the numbers of the array nums are positive integers, I expected k=1 to possibly return >0. However, in the solution, when k=1, output was 0.

Show 1 reply