-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarrays_having_product_less_than_k.java
More file actions
41 lines (37 loc) · 1.12 KB
/
Subarrays_having_product_less_than_k.java
File metadata and controls
41 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
class Subarrays_having_product_less_than_k {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextInt();
long arr[] = new long[n];
for (int i = 0; i < arr.length; i++)
arr[i] = sc.nextLong();
System.out.println(countSubArrayProductLessThanK(arr, n, k));
}
public static int countSubArrayProductLessThanK(long nums[], int n, long k) {
if (k <= 1)
return 0;
int ans = 0;
long prod = nums[0];
if (prod < k)
ans++;
int left = 0;
int right = 1;
while (right != nums.length) {
long val = nums[right];
prod = prod * val;
if (prod < k) {
ans += right - left + 1;
} else {
while (prod >= k) {
prod = prod / nums[left];
left++;
}
ans += right - left + 1;
}
right++;
}
return ans;
}
}