package leetcode;

/**
 * https://leetcode.com/problems/subarray-product-less-than-k/
 */
public class Problem713 {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        int result = 0;
        int totalProduct = 1;
        for (int i = 0; i < nums.length; i++) {
            totalProduct *= nums[i];
        }
        if (totalProduct < k) {
            result++;
        }
        for (int i = 0, j = nums.length - 1; i < j; i++, j--) {
            int leftProduct = 1;
            for (int x = i; x < j; x++) {
                leftProduct *= nums[x];
                if (leftProduct < k) {
                    result++;
                }
                if (totalProduct / leftProduct < k) {
                    result++;
                }
            }
            totalProduct /= nums[i];
        }
        return result;
    }

    public static void main(String[] args) {
        Problem713 prob = new Problem713();
        System.out.println(prob.numSubarrayProductLessThanK(new int[]{10, 5, 2, 6}, 100)); // 8
    }
}

@@ -322,32 +322,100 @@
       %7D%0A        
+for (int i = 0, j = nums.length - 1; i %3C j; i++, j--) %7B%0A            
 if (totalProduct
@@ -425,30 +425,8 @@
 k) %7B
-%0A            result++;
 %0A   
@@ -426,34 +426,32 @@
 ) %7B%0A        
-%7D%0A
         
 for (int i =
@@ -442,63 +442,31 @@
     
-for (int i = 0, j = nums.length - 1; i %3C j; i++, j--) %7B
+result++;%0A            %7D
 %0A   
@@ -809,32 +809,69 @@
 uct /= nums%5Bi%5D;%0A
+            totalProduct /= nums%5Bj%5D;%0A
         %7D%0A      
@@ -983,16 +983,16 @@
 m713();%0A
-
         
@@ -1079,16 +1079,205 @@
 ); // 8%0A
+        System.out.println(prob.numSubarrayProductLessThanK(new int%5B%5D%7B1, 1, 1, 1%7D, 10)); // 10%0A        System.out.println(prob.numSubarrayProductLessThanK(new int%5B%5D%7B2, 1, 3, 5%7D, 10)); // 7%0A
     %7D%0A%7D%0A

Update problem 713