Given an array of positive numbers, calculate the number of possible contiguous subarrays having product lesser than a given number k.
Input : arr[] = [1, 2, 3, 4] , k = 10
Output : 7
Explanation: The subarrays are [1], [2], [3], [4], [1, 2], [1, 2, 3] and [2, 3]Input : arr[] = [1, 9, 2, 8, 6, 4, 3], k = 100
Output : 16Input : arr[] = [10, 5, 2, 6] , k = 100
Output : 8
Table of Content
[Naive Approach] Sliding Window with Product - O(n²) Time and O(1) Space
Count all subarrays whose product is less than k. For each starting index, extend window to the right while product remains < k. Once product exceeds k, break for that starting index.
- Initialize count = 0.
- For i from 0 to n-1: Set product = arr[i] and If product < k, increment count.
- For j from i+1 to n-1: Multiply product by arr[j]. If product < k, increment count. Else break inner loop.
- Return count.
#include <iostream>
#include <vector>
using namespace std;
int countSubarray(vector<int>& arr, int k)
{
int count = 0;
for (int i = 0; i < arr.size(); i++)
{
long long product = arr[i];
// Single-element subarray
if (product < k)
count++;
// Extend the subarray
for (int j = i + 1; j < arr.size(); j++)
{
product *= arr[j];
// Count the subarray if product is less than k
if (product < k)
count++;
else
break;
}
}
return count;
}
int main()
{
vector<int> arr = {1, 2, 3, 4};
int k = 10;
cout << countSubarray(arr, k) << "\n";
return 0;
}
import java.util.*;
class GFG {
static int countSubarray(int[] arr, int k) {
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
long product = arr[i];
// Single-element subarray
if (product < k)
cnt++;
// Extend the subarray
for (int j = i + 1; j < arr.length; j++) {
product *= arr[j];
// Count the subarray if product is less than k
if (product < k)
cnt++;
else
break;
}
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int k = 10;
System.out.println(countSubarray(arr, k));
}
}
# Python program to count subarrays with product less than k
def countSubarray(arr, k):
count = 0
for i in range(len(arr)):
product = arr[i]
# Single-element subarray
if product < k:
count += 1
# Extend the subarray
for j in range(i + 1, len(arr)):
product *= arr[j]
# Count the subarray if product is less than k
if product < k:
count += 1
else:
break
return count
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 4]
k = 10
print(countSubarray(arr, k))
using System;
class GFG {
static int countSubarray(int[] arr, int k) {
int cnt = 0;
for (int i = 0; i < arr.Length; i++) {
long product = arr[i];
// Single-element subarray
if (product < k)
cnt++;
// Extend the subarray
for (int j = i + 1; j < arr.Length; j++) {
product *= arr[j];
// Count the subarray if product is less than k
if (product < k)
cnt++;
else
break;
}
}
return cnt;
}
static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4 };
int k = 10;
Console.WriteLine(countSubarray(arr, k));
}
}
// JavaScript program to count subarrays with product less than k
function countSubarray(arr, k) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
let product = arr[i];
// Single-element subarray
if (product < k)
count++;
// Extend the subarray
for (let j = i + 1; j < arr.length; j++) {
product *= arr[j];
// Count the subarray if product is less than k
if (product < k)
count++;
else
break;
}
}
return count;
}
// Driver code
const arr = [1, 2, 3, 4];
const k = 10;
console.log(countSubarray(arr, k));
Output
7
[Expected Approach] Using Sliding Window Approach - O(n) time and O(1) space
Maintain a sliding window where the product of all elements inside is always less than k. For each position as window end, the number of valid subarrays ending at that position equals the current window length. Expand window by moving end, shrink from start if product becomes ≥ k.
- Initialize product = 1, count = 0, start = 0
- Iterate end from 0 to n-1
- Multiply product by arr[end]
- While product ≥ k and start ≤ end, divide product by arr[start] and increment start
- Add (end - start + 1) to count
- Return count
#include <iostream>
#include <vector>
using namespace std;
int countSubarray(const vector<int>& arr, int k)
{
int n = arr.size();
long long product = 1;
int count = 0;
for (int start = 0, end = 0; end < n; end++)
{
// Add current element to the window product
product *= arr[end];
// Shrink the window until product becomes less than k
while (start <= end && product >= k)
{
product /= arr[start];
start++;
}
// Count all valid subarrays ending at 'end'
count += (end - start + 1);
}
return count;
}
int main()
{
vector<int> arr = {1, 2, 3, 4};
int k = 10;
cout << countSubarray(arr, k) ;
return 0;
}
import java.util.*;
class GFG {
static int countSubarray(int[] arr, int k) {
int n = arr.length;
long product = 1;
int count = 0;
for (int start = 0, end = 0; end < n; end++) {
// Add current element to the window product
product *= arr[end];
// Shrink the window until product becomes less than k
while (start <= end && product >= k) {
product /= arr[start];
start++;
}
// Count all valid subarrays ending at 'end'
count += (end - start + 1);
}
return count;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int k = 10;
System.out.println(countSubarray(arr, k));
}
}
def countSubarray(arr, k):
n = len(arr)
product = 1
count = 0
start = 0
for end in range(n):
# Add current element to the window product
product *= arr[end]
# Shrink the window until product becomes less than k
while start <= end and product >= k:
product //= arr[start]
start += 1
# Count all valid subarrays ending at 'end'
count += (end - start + 1)
return count
if __name__ == "__main__":
arr = [1, 2, 3, 4]
k = 10
print(countSubarray(arr, k))
using System;
class GFG {
static int countSubarray(int[] arr, int k) {
int n = arr.Length;
long product = 1;
int count = 0;
for (int start = 0, end = 0; end < n; end++) {
// Add current element to the window product
product *= arr[end];
// Shrink the window until product becomes less than k
while (start <= end && product >= k) {
product /= arr[start];
start++;
}
// Count all valid subarrays ending at 'end'
count += (end - start + 1);
}
return count;
}
static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4 };
int k = 10;
Console.WriteLine(countSubarray(arr, k));
}
}
function countSubarray(arr, k) {
const n = arr.length;
let product = 1;
let count = 0;
let start = 0;
for (let end = 0; end < n; end++) {
// Add current element to the window product
product *= arr[end];
// Shrink the window until product becomes less than k
while (start <= end && product >= k) {
product /= arr[start];
start++;
}
// Count all valid subarrays ending at 'end'
count += (end - start + 1);
}
return count;
}
// Driver code
const arr = [1, 2, 3, 4];
const k = 10;
console.log(countSubarray(arr, k));
Output
7