|
| 1 | +--- |
| 2 | +id: subarray-product-less-than-k |
| 3 | +title: Subarray Product Less Than K |
| 4 | +sidebar_label: 713. Subarray Product Less Than K |
| 5 | + |
| 6 | +tags: |
| 7 | +- Array |
| 8 | +- Sliding Window |
| 9 | + |
| 10 | +description: "This is a solution to the Subarray Product Less Than K problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | +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. |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | +``` |
| 19 | +Input: nums = [10,5,2,6], k = 100 |
| 20 | +Output: 8 |
| 21 | +Explanation: The 8 subarrays that have product less than 100 are: |
| 22 | +[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] |
| 23 | +Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k. |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | +``` |
| 28 | +Input: nums = [1,2,3], k = 0 |
| 29 | +Output: 0 |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +### Constraints |
| 35 | +- `1 <= nums.length <= 3 * 10^4` |
| 36 | +- `1 <= nums[i] <= 1000` |
| 37 | +- `0 <= k <= 10^6` |
| 38 | + |
| 39 | +## Solution for Path With Minimum Effort Problem |
| 40 | +### Approach |
| 41 | +#### Brute Force |
| 42 | +- Generate All the Subarray and Check whether the which subarray has product less than K. |
| 43 | + |
| 44 | +#### Optimized Approach - Sliding Window |
| 45 | +##### Initialize Pointers and Variables: |
| 46 | + |
| 47 | +- Use two pointers, i and j, to represent the start and end of the current subarray, respectively. |
| 48 | +- Initialize prod to 1 to keep track of the product of elements in the current window. |
| 49 | +- Initialize cnt to 0 to count the number of valid subarrays. |
| 50 | +##### Expand the Window: |
| 51 | + |
| 52 | +- Start with both pointers at the beginning of the array. Expand the window by moving the j pointer to the right, multiplying the product prod by the new element nums[j]. |
| 53 | +##### Check the Product: |
| 54 | + |
| 55 | +- If the product prod is less than k, all subarrays ending at j and starting from any position between i and j are valid. Therefore, add j - i + 1 to cnt. |
| 56 | +Shrink the Window: |
| 57 | + |
| 58 | +- If the product prod is greater than or equal to k, move the i pointer to the right until the product is less than k. Each time you move i, divide prod by nums[i]. |
| 59 | +##### Repeat: |
| 60 | + |
| 61 | +- Continue expanding the window with j and adjusting i as needed until j reaches the end of the array. |
| 62 | +##### Return the Result: |
| 63 | + |
| 64 | +- The total count cnt is the number of subarrays with a product less than k. |
| 65 | + |
| 66 | + |
| 67 | +<Tabs> |
| 68 | + <TabItem value="Solution" label="Solution"> |
| 69 | + |
| 70 | + #### Implementation |
| 71 | + ```jsx live |
| 72 | + function Solution(arr) { |
| 73 | + var numSubarrayProductLessThanK = function(nums, k) { |
| 74 | + let i = 0; |
| 75 | + let j = 0; |
| 76 | + let cnt = 0; |
| 77 | + let prod = 1; |
| 78 | + while (j < nums.length) { |
| 79 | + prod *= nums[j]; |
| 80 | + if (prod < k) { |
| 81 | + cnt += j - i + 1; |
| 82 | + } else { |
| 83 | + while (prod >= k && i <= j) { |
| 84 | + prod /= nums[i]; |
| 85 | + i++; |
| 86 | + } |
| 87 | + if (prod < k) { |
| 88 | + cnt += j - i + 1; |
| 89 | + } |
| 90 | + } |
| 91 | + j++; |
| 92 | + } |
| 93 | + return cnt; |
| 94 | + }; |
| 95 | + |
| 96 | + const input =[10,5,2,6] |
| 97 | + const k = 100 |
| 98 | + const output = numSubarrayProductLessThanK(input , k) |
| 99 | + return ( |
| 100 | + <div> |
| 101 | + <p> |
| 102 | + <b>Input: </b> |
| 103 | + {JSON.stringify(input)} |
| 104 | + </p> |
| 105 | + <p> |
| 106 | + <b>Output:</b> {output.toString()} |
| 107 | + </p> |
| 108 | + </div> |
| 109 | + ); |
| 110 | + } |
| 111 | + ``` |
| 112 | + |
| 113 | + #### Complexity Analysis |
| 114 | + |
| 115 | + - Time Complexity: $ O(N) $ |
| 116 | + - Space Complexity: $ O(1)$ |
| 117 | + |
| 118 | + ## Code in Different Languages |
| 119 | + <Tabs> |
| 120 | + <TabItem value="JavaScript" label="JavaScript"> |
| 121 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 122 | + ```javascript |
| 123 | + var numSubarrayProductLessThanK = function(nums, k) { |
| 124 | + let i = 0; |
| 125 | + let j = 0; |
| 126 | + let cnt = 0; |
| 127 | + let prod = 1; |
| 128 | + while (j < nums.length) { |
| 129 | + prod *= nums[j]; |
| 130 | + if (prod < k) { |
| 131 | + cnt += j - i + 1; |
| 132 | + } else { |
| 133 | + while (prod >= k && i <= j) { |
| 134 | + prod /= nums[i]; |
| 135 | + i++; |
| 136 | + } |
| 137 | + if (prod < k) { |
| 138 | + cnt += j - i + 1; |
| 139 | + } |
| 140 | + } |
| 141 | + j++; |
| 142 | + } |
| 143 | + return cnt; |
| 144 | +}; |
| 145 | + |
| 146 | + ``` |
| 147 | + |
| 148 | + </TabItem> |
| 149 | + <TabItem value="TypeScript" label="TypeScript"> |
| 150 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 151 | + ```typescript |
| 152 | + function numSubarrayProductLessThanK(nums: number[], k: number): number { |
| 153 | + let i = 0; |
| 154 | + let j = 0; |
| 155 | + let cnt = 0; |
| 156 | + let prod = 1; |
| 157 | + while (j < nums.length) { |
| 158 | + prod *= nums[j]; |
| 159 | + if (prod < k) { |
| 160 | + cnt += j - i + 1; |
| 161 | + } else { |
| 162 | + while (prod >= k && i <= j) { |
| 163 | + prod /= nums[i]; |
| 164 | + i++; |
| 165 | + } |
| 166 | + if (prod < k) { |
| 167 | + cnt += j - i + 1; |
| 168 | + } |
| 169 | + } |
| 170 | + j++; |
| 171 | + } |
| 172 | + return cnt; |
| 173 | +} |
| 174 | +
|
| 175 | + ``` |
| 176 | + </TabItem> |
| 177 | + <TabItem value="Python" label="Python"> |
| 178 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 179 | + ```python |
| 180 | + class Solution: |
| 181 | + def numSubarrayProductLessThanK(self, nums, k): |
| 182 | + i = 0 |
| 183 | + j = 0 |
| 184 | + cnt = 0 |
| 185 | + prod = 1 |
| 186 | + while j < len(nums): |
| 187 | + prod *= nums[j] |
| 188 | + if prod < k: |
| 189 | + cnt += j - i + 1 |
| 190 | + else: |
| 191 | + while prod >= k and i <= j: |
| 192 | + prod /= nums[i] |
| 193 | + i += 1 |
| 194 | + if prod < k: |
| 195 | + cnt += j - i + 1 |
| 196 | + j += 1 |
| 197 | + return cnt |
| 198 | +
|
| 199 | + ``` |
| 200 | + |
| 201 | + </TabItem> |
| 202 | + <TabItem value="Java" label="Java"> |
| 203 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 204 | + ```java |
| 205 | + class Solution { |
| 206 | + public int numSubarrayProductLessThanK(int[] nums, int k) { |
| 207 | + int i = 0; |
| 208 | + int j = 0; |
| 209 | + long cnt = 0; |
| 210 | + long prod = 1; |
| 211 | + while (j < nums.length) { |
| 212 | + prod *= nums[j]; |
| 213 | + if (prod < k) { |
| 214 | + cnt += j - i + 1; |
| 215 | + } else { |
| 216 | + while (prod >= k && i <= j) { |
| 217 | + prod /= nums[i]; |
| 218 | + i++; |
| 219 | + } |
| 220 | + if (prod < k) { |
| 221 | + cnt += j - i + 1; |
| 222 | + } |
| 223 | + } |
| 224 | + j++; |
| 225 | + } |
| 226 | + return (int) cnt; |
| 227 | + } |
| 228 | +} |
| 229 | +
|
| 230 | + ``` |
| 231 | + |
| 232 | + </TabItem> |
| 233 | + <TabItem value="C++" label="C++"> |
| 234 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 235 | + ```cpp |
| 236 | + class Solution { |
| 237 | +public: |
| 238 | + int numSubarrayProductLessThanK(vector<int>& nums, int k) { |
| 239 | + int i=0; |
| 240 | + int j=0; |
| 241 | + long long cnt = 0; |
| 242 | + long long prod = 1; |
| 243 | + while(j<nums.size()) |
| 244 | + { |
| 245 | + prod=prod *1LL*nums[j]; |
| 246 | + if(prod <k) |
| 247 | + { |
| 248 | + cnt+= j-i+1; |
| 249 | + } |
| 250 | + else if(prod>=k) |
| 251 | + { |
| 252 | + while(prod>=k && i<=j) |
| 253 | + { |
| 254 | + prod/=nums[i]; |
| 255 | + i++; |
| 256 | + } |
| 257 | +
|
| 258 | + if(prod<k) |
| 259 | + { |
| 260 | + cnt+=j-i+1; |
| 261 | + } |
| 262 | + } |
| 263 | + j++; |
| 264 | + } |
| 265 | + return (int)cnt ; |
| 266 | + } |
| 267 | +}; |
| 268 | +
|
| 269 | + ``` |
| 270 | +</TabItem> |
| 271 | +</Tabs> |
| 272 | + |
| 273 | + </TabItem> |
| 274 | +</Tabs> |
| 275 | + |
| 276 | +## References |
| 277 | + |
| 278 | +- **LeetCode Problem**: [Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/description/) |
| 279 | + |
| 280 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/subarray-product-less-than-k/solutions) |
| 281 | + |
0 commit comments