Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update count-the-subarrays-having-product-less-than-k.cpp #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ using namespace std;

class Solution{
public:
int countSubArrayProductLessThanK(const vector<int>& a, int n, long long k) {
int i = 0, j = 0, ans = 0;
long long prod = 1;
while(i < n && j < n){
prod *= a[j];
while(prod >= k && i <= j){
prod /= a[i++];
long long countSubArrayProductLessThanK(const vector<int>& a, int n, long long k) {
long long p = 1;
long long res = 0;
for (int start = 0, end = 0; end < n; end++) {

// Move right bound by 1 step. Update the product.
p *= a[end];

// Move left bound so guarantee that p is again
// less than k.
while (start < end && p >= k) p /= a[start++];

// If p is less than k, update the counter.
// Note that this is working even for (start == end):
// it means that the previous window cannot grow
// anymore and a single array element is the only
// addendum.
if (p < k) {
int len = end - start + 1;
res += len;
}
ans += (j-i+1);
j++;
}
return ans;

return res;
}
};

Expand All @@ -40,4 +52,4 @@ int main() {
return 0;
}

// } Driver Code Ends
// } Driver Code Ends