I was trying to write the solution for this question following the sliding window pattern.
for each iteration I lock the left position and slide right until the product is less than target.
We can slide the right pointer safely , since we now the array can contain only positive numbers
With this approach we do it in O(n^2), Am I doing anything wrong ?
public static List<List<Integer>> findSubarrays(int[] arr, int target) {
List<List<Integer>> result = new ArrayList<>();
int product;
int left = 0;
while (left < arr.length) {
if (arr[left] < target) {
int right = left;
List<Integer> tempList = new ArrayList<>();
product = arr[right];
while (product < target && right < arr.length) {
tempList.add(arr[right]);
result.add(new ArrayList<>(tempList));
right++;
if (right < arr.length) {
product *= arr[right];
}
}
}
left++;
}
return result;
}