Created
December 24, 2017 22:38
-
-
Save thmain/d4c35ec77d020f5c629464060b213008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SubarrayProductLessThanK_Better { | |
public int countSubArraysProudctLessThanK(int [] arr, int k){ | |
int start = 0; | |
int end = 1; | |
long product = arr[0]; | |
int count = 0; | |
while (start <= end && end <= arr.length) { | |
if (product < k) { | |
count += end - start; | |
if (end < arr.length) product *= arr[end]; | |
end++; | |
} else { | |
product /= arr[start++]; | |
} | |
} | |
return count; | |
} | |
public static void main(String[] args) { | |
SubarrayProductLessThanK_Better s = new SubarrayProductLessThanK_Better(); | |
int [] nums = {10,4,2,6}; | |
int k = 100; | |
System.out.println("No of sub arrays has sum less than k="+k+" are: " + | |
s.countSubArraysProudctLessThanK(nums,k)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment