-
Notifications
You must be signed in to change notification settings - Fork 5
/
CountSubArrays.java
51 lines (43 loc) · 1.54 KB
/
CountSubArrays.java
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
42
43
44
45
46
47
48
49
50
51
/*https://practice.geeksforgeeks.org/problems/count-the-subarrays-having-product-less-than-k1708/1*/
//using sliding window
class Solution {
public int countSubArrayProductLessThanK(long a[], long n, long k)
{
int lastAdded = -1;
int start = 0, end = -1;
double product = 1;
int count = 0;
while (start < n && end < n)
{
//extend the window
while (end+1 < n && product*a[end+1] < k)
{
++end;
product *= a[end];
}
//still if the window is reversed, start a new window from next position
if (start > end)
{
lastAdded = start;
++start; ++end;
product = 1;
continue;
}
//current window length
int windowLength = end-start+1;
//window length of the already added elements
int addedWindowLength = lastAdded-start+1;
//calculate the number of subarrays and add it
int totalTillNow = (int)(((double)windowLength*((double)windowLength+1)/2)-((double)addedWindowLength*((double)addedWindowLength+1)/2));
count += totalTillNow;
//mark the end of current window as the last added element
lastAdded = end;
//slide the window to right
product /= a[start];
++start;
//if window is reversed, reset product
if (start > end) product = 1;
}
return count;
}
}