diff --git a/Java/src/net/kenyang/algorithm/SubArrayProduct b/Java/src/net/kenyang/algorithm/SubArrayProduct new file mode 100644 index 0000000000..f0bb90bb77 --- /dev/null +++ b/Java/src/net/kenyang/algorithm/SubArrayProduct @@ -0,0 +1,19 @@ +public class SubArrayProduct { + public int numSubarrayProductLessThanK(int[] nums, int k) { + int counter_less_than_k = 0; + int product = 1; + int head = 0, tail = 0; + + while (head < nums.length && tail <= head) { + product *= nums[head]; + + while (product >= k && tail <= head) { + product = product / nums[tail]; + tail++; + } + counter_less_than_k += head - tail + 1; + head++; + } + return counter_less_than_k; + } +} \ No newline at end of file