Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Hashing & Two Pointers

Subarrays with Product Less Than K

Medium · Hashing & Two Pointers · Asked at Amazon, Google, Microsoft, Bloomberg

All numbers are positive. Print how many contiguous subarrays have a product strictly less than k.

Input

First line: n k. Second line: n integers.

Output

The number of such subarrays.

Constraints

Sliding window: extend the right end multiplying the product; while the product is ≥ k, divide out the left element and move left. Every window [l, r] that is valid contributes r − l + 1 subarrays ending at r. The count can pass 2^31.

Example 1

Input

4 100
10 5 2 6

Output

8

Example 2

Input

3 0
1 2 3

Output

0

Hints

  1. All numbers are positive, so extending a window only increases the product.
  2. For each right end r, find the smallest left end l with product < k.
  3. Every start from l to r gives a valid subarray ending at r.

Approach

Sliding window: multiply a[r] into the product; while product ≥ k (and l ≤ r), divide out a[l] and move l right. Then add r − l + 1. If k ≤ 1 no positive product is below k, so the answer is 0 (handle it first — otherwise the loop would empty the window forever). The count can reach about 5 × 10^9, so use 64 bits.

Complexity: O(n) time · O(1) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    long long k;
    cin >> n >> k;
    vector<long long> a(n);
    for (auto &x : a) cin >> x;
    if (k <= 1) {
        cout << 0 << "\n";
        return 0;
    }
    long long prod = 1, count = 0;
    int l = 0;
    for (int r = 0; r < n; r++) {
        prod *= a[r];
        while (prod >= k) prod /= a[l++];
        count += r - l + 1;
    }
    cout << count << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, k = data[0], data[1]
a = data[2 : 2 + n]
if k <= 1:
    print(0)
else:
    prod, l, count = 1, 0, 0
    for r in range(n):
        prod *= a[r]
        while prod >= k:
            prod //= a[l]
            l += 1
        count += r - l + 1
    print(count)

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        long k = (long) in.nval;
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (long) in.nval;
        }
        if (k <= 1) {
            System.out.println(0);
            return;
        }
        long prod = 1, count = 0;
        int l = 0;
        for (int r = 0; r < n; r++) {
            prod *= a[r];
            while (prod >= k) prod /= a[l++];
            count += r - l + 1;
        }
        System.out.println(count);
    }
}

Solve Subarrays with Product Less Than K online · All practice problems