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++
// 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;
}
Java
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));
}
}
Python3
# 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))
C#
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));
}
}
JavaScript
// 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));
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.
Similar Reads
Count of subarrays whose maximum element is greater than k Given an array of n elements and an integer k. The task is to find the count of the subarray which has a maximum element greater than K. Examples : Input : arr[] = {1, 2, 3} and k = 2.Output : 3All the possible subarrays of arr[] are { 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, { 1, 2, 3 }.Their maximum
15+ min read
Count subarrays with all elements greater than K Given an array of n integers and an integer k, the task is to find the number of subarrays such that all elements in each subarray are greater than k.Examples: Input: arr[] = {3, 4, 5, 6, 7, 2, 10, 11}, k= 5 Output: 6 The possible subarrays are {6}, {7}, {6, 7}, {10}, {11} and {10, 11}.Input: arr[]
9 min read
Smallest subarray such that all elements are greater than K Given an array of N integers and a number K, the task is to find the length of the smallest subarray in which all the elements are greater than K. If there is no such subarray possible, then print -1. Examples: Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5 Output: 1 The subarray is {10} Input: a[]
4 min read
Count of subarrays of size K with average at least M Given an array arr[] consisting of N integers and two positive integers K and M, the task is to find the number of subarrays of size K whose average is at least M. Examples: Input: arr[] = {2, 3, 3, 4, 4, 4, 5, 6, 6}, K = 3, M = 4Output: 4Explanation:Below are the subarrays of size K(= 3) whose aver
7 min read
Longest Subarray having Majority Elements Greater Than K Given an array arr[] and an integer k, the task is to find the length of longest subarray in which the count of elements greater than k is more than the count of elements less than or equal to k.Examples:Input: arr[]= [1, 2, 3, 4, 1], k = 2Output: 3 Explanation: The subarray [2, 3, 4] or [3, 4, 1] s
13 min read