Given an array of N elements, K is the minimum size of the sub-array, and M is the limit of every element in the sub-array, the task is to count the number of sub-arrays with a minimum size of K and all the elements are lesser than or equal to M.
Examples:
Input: arr[] = {-5, 0, -10}, K = 1, M = 15
Output: 6
Explanation: The sub-arrays [-5], [0], [-10], [-5, 0], [0, -10] and [-5, 0, -10] have all elements <= 15 and size>=1.Input: arr[] = {0, 3, -2, 5, -4, -4}, K = 2, M = 3
Output: 4
Explanation: The sub-arrays are [0, 3], [3,-2], [-4, -4], and [0, 3, -2] have a size of >= K and all elements <=M.
Naive approach: The basic way to solve the problem is as follows:
The idea is to iterate over all the subarrays using nested loops and then check for given conditions if it is satisfied then add it to the answer.
Time Complexity: O(N^2), where N is the size of the array.
Auxiliary Space: O(1)
Efficient Approach: To solve the problem follow the below steps:
- The number of total subarrays of length N is N*(N+1)/2.
- Calculate the maximum length of the array such that all elements are less than equal to M.
- The task is to count the number of subarrays of size at least K out of length we have found.
- Let cnt be the number of consecutive elements counts where all elements are lesser or equal to M.
- The problem is to count the number of subarrays of size at least K out of cnt, we can modify the formulae to be as follows:
(cnt - K + 1)*((cnt - K + 1) + 1)/2
Below is the implementation of the above approach:
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to calculate N*(N+1)/2
long countSubarrys(long n) { return n * (n + 1) / 2; }
// Function to count the number of sub-arrays
// with atleast K size and elements
// not greater than M.
long countSubarrays(int a[], int N, int K, int M)
{
long res = 0;
// counts curr elements which are <= M
long cnt = 0;
for (int i = 0; i < N; i++) {
// Go on counting the valid elements
if (a[i] <= M) {
cnt += 1;
}
// As the current ele is > M
else {
// Next condition is the
// sub-array should be >= K
if (cnt >= K) {
// Next condition is the
// sub-array should be >= K
// Formula to get the
// number of sub-arrays
res += countSubarrys(cnt - K + 1);
}
cnt = 0;
}
}
// Check our cnt is valid and count the
// sub-arrays if cnt >= K
if (cnt >= K) {
res += countSubarrys(cnt - K + 1);
}
return res;
}
// Drivers code
int main()
{
int a[] = { 2, 0, 11, 3, 0 };
int N = sizeof(a) / sizeof(a[0]);
int K = 2, M = 3;
// Function call
cout << countSubarrays(a, N, K, M);
return 0;
}
class GFG {
// function to calculate N*(N+1)/2
static long countSubarrys(long n) {
return n * (n + 1) / 2;
}
// function to count the number of sub-arrays with
// atleast K size and elements not greater than M.
static long countSubarrays(int a[], int N, int K, int M) {
long res = 0;
long cnt = 0; // counts curr elements which are <= M
for (int i = 0; i < N; i++) {
if (a[i] <= M) {
cnt += 1; // go on counting the valid elements
} else { // As the current ele is > M
if (cnt >= K) { // next condition is the sub-array should be >= K
res += countSubarrys(cnt - K + 1); // formula to get the number of sub-arrays
}
cnt = 0;
}
}
// check our cnt is valid and count the sub-arrays
if (cnt >= K) {
res += countSubarrys(cnt - K + 1);
}
return res;
}
public static void main(String[] args) {
int[] a = { 2, 0, 11, 3, 0 };
int N = a.length;
int K = 2, M = 3;
System.out.println(countSubarrays(a, N, K, M));
}
}
# function to calculate N*(N+1)/2
def countSubarrys(n):
return n * (n + 1) // 2
# function to count the number of sub-arrays with
# atleast K size and elements not greater than M.
def countSubarrays(a, N, K, M):
res = 0
cnt = 0
for i in range(N):
if a[i] <= M:
cnt += 1
else:
if cnt >= K:
res += countSubarrys(cnt - K + 1)
cnt = 0
if cnt >= K:
res += countSubarrys(cnt - K + 1)
return res
# Driver code
a = [2, 0, 11, 3, 0]
N = len(a)
K = 2
M = 3
print(countSubarrays(a, N, K, M))
using System;
class Program
{
// Function to calculate N*(N+1)/2
static long CountSubarrays(long n) => n * (n + 1) / 2;
// Function to count the number of sub-arrays
// with at least K size and elements
// not greater than M.
static long CountSubarrays(int[] a, int N, int K, int M)
{
long res = 0;
// Count current elements which are <= M
long cnt = 0;
for (int i = 0; i < N; i++)
{
// Go on counting the valid elements
if (a[i] <= M)
{
cnt += 1;
}
// As the current ele is > M
else
{
// Next condition is the sub-array should be >= K
if (cnt >= K)
{
// Next condition is the sub-array should be >= K
// Formula to get the number of sub-arrays
res += CountSubarrays(cnt - K + 1);
}
cnt = 0;
}
}
// Check if cnt is valid and count the sub-arrays if cnt >= K
if (cnt >= K)
{
res += CountSubarrays(cnt - K + 1);
}
return res;
}
// Main method
static void Main()
{
int[] a = { 2, 0, 11, 3, 0 };
int N = a.Length;
int K = 2, M = 3;
// Function call
Console.WriteLine(CountSubarrays(a, N, K, M));
}
}
// function to calculate N*(N+1)/2
function countSubarrys(n) {
return (n * (n + 1)) / 2;
}
// function to count the number of sub-arrays with
// atleast K size and elements not greater than M.
function countSubarrays(a, N, K, M) {
let res = 0;
let cnt = 0;
for (let i = 0; i < N; i++) {
if (a[i] <= M) {
cnt += 1;
} else {
if (cnt >= K) {
res += countSubarrys(cnt - K + 1);
}
cnt = 0;
}
}
if (cnt >= K) {
res += countSubarrys(cnt - K + 1);
}
return res;
}
// Driver code
var a = [2, 0, 11, 3, 0];
var N = a.length;
var K = 2;
var M = 3;
document.write(countSubarrays(a, N, K, M));
Output
2
Time Complexity: O(N), as we are using a loop to traverse N elements.
Auxiliary Space: O(1), as we are not using any extra space.