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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method involves checking every possible group of numbers to see if their product is less than a target value. We'll look at all possible starting points and then consider groups of increasing size from each of those points.
Here's how the algorithm would work step-by-step:
def sub_array_product_less_than_k_brute_force(numbers, target):
count = 0
# Iterate through each starting index of the subarray
for start_index in range(len(numbers)):
current_product = 1
# Iterate through each possible end index for the subarray
for end_index in range(start_index, len(numbers)):
current_product *= numbers[end_index]
# Check if the current product is less than the target
if current_product < target:
count += 1
return count
The fastest way to solve this is to use a 'sliding window' technique. We expand a range of numbers until their product is too big, then shrink it back down until it's small enough again. By keeping track of the size of these valid ranges, we can count all the subarrays quickly.
Here's how the algorithm would work step-by-step:
def subArrayProductLessThanK(numbers, target):
if target <= 1:
return 0
left_index = 0
current_product = 1
sub_array_count = 0
for right_index, number in enumerate(numbers):
current_product *= number
# Shrink the window if product too large
while current_product >= target:
current_product /= numbers[left_index]
left_index += 1
# Maintain window invariant
# Add the number of valid subarrays ending at right_index.
sub_array_count += right_index - left_index + 1
return sub_array_count
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately because there are no subarrays to evaluate. |
k is less than or equal to 0 | Return 0 immediately since product of positive numbers is always positive and therefore never less than a non-positive k. |
Array contains a single element and k is greater than the element | Return 1, as the single element subarray satisfies the condition. |
Array contains a single element and k is less than or equal to the element | Return 0, as the single element subarray does not satisfy the condition. |
Array contains all 1s and k is greater than 1 | The number of subarrays is n*(n+1)/2 where n is array length. |
Array contains very large numbers such that product easily overflows | Use long data type to prevent integer overflow during product calculation. |
k is a very large number, close to the maximum value that integer can hold. | The sliding window might expand until integer overflow or memory exhaustion occurs, necessitating long data type use. |
All numbers in array are greater than or equal to k | Return 0, as no subarray will have a product less than k. |