mediumTwo PointersPattern: Sliding Window

Count Contiguous Subarrays Below Threshold Solution

Problem Statement

Given an array of integers `values` and two integers `n` and `k`, count all contiguous subarrays of size `n` where the product of its elements is less than `k`.

Constraints

  • 1 <= n <= 3 * 10^4
  • 1 <= arr[i] <= 1000
  • 0 <= k <= 10^6
Time: O(N) Space: O(1)
Two pointers. Expand right, multiply product. While product >= k, divide by left element and shrink left. Number of valid subarrays ending at 'right' is (right - left + 1). Sum these up. Time O(N), Space O(1).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.