Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active December 24, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/33a85f0af1e47dc2c2a472b2aa1aae66 to your computer and use it in GitHub Desktop.
Save thmain/33a85f0af1e47dc2c2a472b2aa1aae66 to your computer and use it in GitHub Desktop.
public class SubarrayProductLessThanK {
public int find(int [] arrA, int k){
int result=0;
int arrSize = arrA.length;
//start point
for (int startPoint = 0; startPoint <arrSize ; startPoint++) {
//group sizes
for (int grps = startPoint; grps <=arrSize ; grps++) {
//if start point = 1 then
//grp size = 1 , take 10
//grp size = 2, take 10 4
//grp size = 3, take 10 4 2 ans so on
int product=1;
int noElements=0;
String tempSubArrrays ="";
for (int j = startPoint ; j < grps ; j++) {
tempSubArrrays += arrA[j] + " ";
product *= arrA[j];
noElements++;
}
if(product<k && noElements>0){
System.out.print(tempSubArrrays);
result++;
System.out.println();
}
}
}
return result;
}
public static void main(String[] args) {
SubarrayProductLessThanK s = new SubarrayProductLessThanK();
int [] nums = {10,4,2,6};
int k = 100;
System.out.println("Sub arrays has sum less than k=100 are: " + s.find(nums, k));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment