-
Notifications
You must be signed in to change notification settings - Fork 3
/
Subarray_Product_Less_Than_K.cpp
103 lines (100 loc) · 2.35 KB
/
Subarray_Product_Less_Than_K.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Subarray Product Less Than K
problem -> https://leetcode.com/problems/subarray-product-less-than-k/
Your are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray
is less than k.
Example 1:
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2],
[2, 6], [5, 2, 6].
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
*/
class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& a, int k)
{
int size=a.size();
long int c=0;
if(k<=1)
{
return 0;
}
long int p=1,s=0,e1=-1,e,no,f=0;
for(int i=0;i<size;)
{
if(p*a[i]<k)
{
e=i;
p=p*a[i];
f=1;
i++;
}
else if(f)
{
if(s>e1)
{
no=e-s+1;
c=c+((no)*(no+1))/2;
}
else
{
no=e1-s+1;
c=c+no*(e-e1);
no=e-e1;
c=c+((no)*(no+1))/2;
}
e1=e;
f=0;
}
else if(s<=e)
{
p=p/a[s];
s++;
}
else
{
i++;
s=i;
e=i;
}
}
if(f)
{
if(s>e1)
{
no=e-s+1;
c=c+((no)*(no+1))/2;
}
else
{
no=e1-s+1;
c=c+no*(e-e1);
no=e-e1;
c=c+((no)*(no+1))/2;
}
}
return c;
}
};
/*
Java Soln :->
class Solution
{
public int numSubarrayProductLessThanK(int[] nums, int k)
{
if (k <= 1)
return 0;
int prod = 1, ans = 0, left = 0;
for (int right = 0; right < nums.length; right++)
{
prod *= nums[right];
while (prod >= k)
prod /= nums[left++];
ans += right - left + 1;
}
return ans;
}
}
*/