Subarray Product Less Than K - Complete Solution Guide
Subarray Product Less Than K is LeetCode problem 713, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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:
Detailed Explanation
The problem asks us to find the number of contiguous subarrays within a given array of integers (`nums`) whose product is strictly less than a given integer `k`. A contiguous subarray is a sequence of elements that are adjacent in the array. The input consists of an array of integers `nums` and an integer `k`. The output is the number of subarrays that satisfy the given condition (product less than k). The constraints are that the length of `nums` is between 1 and 3 * 10^4, each element in `nums` is between 1 and 1000, and `k` is between 0 and 10^6.
Solution Approach
The provided solution uses the sliding window technique. We maintain a window defined by `left` and `right` pointers. We iterate through the array with the `right` pointer, expanding the window to include the current element. We update the product by multiplying it by the current element. If the product becomes greater than or equal to `k`, we shrink the window from the left until the product is less than `k`. The number of subarrays ending at the current `right` index is then `right - left + 1`. We add this number to the `count`, which accumulates the total number of subarrays that meet the criteria.
Step-by-Step Algorithm
- Step 1: Initialize `count` to 0, `product` to 1, and `left` to 0. Handle the edge case where `k <= 1` by returning 0.
- Step 2: Iterate through the `nums` array using a `right` pointer.
- Step 3: Multiply the current `product` by the element at the `right` index: `product *= nums[right]`.
- Step 4: While the `product` is greater than or equal to `k`, divide the `product` by the element at the `left` index and increment the `left` pointer: `product /= nums[left]; left++`.
- Step 5: Add the number of subarrays ending at the current `right` index to the `count`: `count += right - left + 1`.
- Step 6: After the loop finishes, return the final `count`.
Key Insights
- Insight 1: The problem can be efficiently solved using a sliding window technique.
- Insight 2: The window needs to expand by including right elements and shrink from the left when the product exceeds k.
- Insight 3: We need to handle the edge case where k is less than or equal to 1, as no subarray can have a product less than k in such a scenario.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search, Sliding Window, Prefix Sum.
Companies
Asked at: Agoda, Airbnb, Flexport, IBM, Nvidia, PayPal, Salesforce, Samsung, ServiceNow, SoFi, Wise.