Number of subarrays with at-least K size and elements not greater than M
Last Updated :
11 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
long countSubarrys( long n) { return n * (n + 1) / 2; }
long countSubarrays( int a[], int N, int K, int M)
{
long res = 0;
long cnt = 0;
for ( int 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;
}
int main()
{
int a[] = { 2, 0, 11, 3, 0 };
int N = sizeof (a) / sizeof (a[0]);
int K = 2, M = 3;
cout << countSubarrays(a, N, K, M);
return 0;
}
|
Java
class GFG {
static long countSubarrys( long n) {
return n * (n + 1 ) / 2 ;
}
static long countSubarrays( int a[], int N, int K, int M) {
long res = 0 ;
long cnt = 0 ;
for ( int 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;
}
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));
}
}
|
Python3
def countSubarrys(n):
return n * (n + 1 ) / / 2
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
a = [ 2 , 0 , 11 , 3 , 0 ]
N = len (a)
K = 2
M = 3
print (countSubarrays(a, N, K, M))
|
C#
using System;
class Program
{
static long CountSubarrays( long n) => n * (n + 1) / 2;
static long CountSubarrays( int [] a, int N, int K, int M)
{
long res = 0;
long cnt = 0;
for ( int i = 0; i < N; i++)
{
if (a[i] <= M)
{
cnt += 1;
}
else
{
if (cnt >= K)
{
res += CountSubarrays(cnt - K + 1);
}
cnt = 0;
}
}
if (cnt >= K)
{
res += CountSubarrays(cnt - K + 1);
}
return res;
}
static void Main()
{
int [] a = { 2, 0, 11, 3, 0 };
int N = a.Length;
int K = 2, M = 3;
Console.WriteLine(CountSubarrays(a, N, K, M));
}
}
|
Javascript
function countSubarrys(n) {
return (n * (n + 1)) / 2;
}
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;
}
var a = [2, 0, 11, 3, 0];
var N = a.length;
var K = 2;
var M = 3;
document.write(countSubarrays(a, N, K, M));
|
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.
Share your thoughts in the comments
Please Login to comment...