Count of subarrays with maximum value as K
Last Updated :
07 Sep, 2021
Given an array arr[] of N integers and an integer K. The task is to find the number of subarrays with a maximum value is equal to K.
Examples:
Input: arr[ ] = {2, 1, 3, 4}, K = 3
Output: 3
Explanation:
Sub-arrays with maximum value is equals K are { 2, 1, 3 }, { 1, 3 }, { 3 }, hence the answer is 3.
Input: arr[ ] = {1, 2, 3}, K = 1.
Output: 1
Approach: The number of subarrays in the array arr[] is equal to the number of subarrays with a maximum not greater than K minus the number of subarrays with a maximum not greater than K-1. Refer here to counting the number of subarrays with a maximum greater than K. Follow the steps below to solve the problem:
- Initialize the variable say, count1 as 0 to calculate the number of subarrays with a maximum not greater than K-1.
- Initialize the variable say, count2 as 0 to calculate the number of subarrays with a maximum not greater than K.
- Define a function say, totalSubarrays(arr, N, K) to count the number of subarrays with a maximum not greater than K and do the following steps:
- Initialize the variable say, ans as 0 to store the count of subarrays with a maximum not greater than K.
- Iterate in the range [0, N-1] and perform the following steps.
- If the value of arr[i] is greater than K, then increase the value of i by 1 and continue.
- Initialize the variable count as 0 to store the total count possible subarrays.
- Iterate in the range till i is less than N and arr[i] is less than equal to K.
- Increase the value of i and count by 1.
- Add the value of (count*(count+1))/2 to the value of ans.
- Return the value of ans.
- Calculate the number of subarrays with a maximum not greater than K-1 by calling function totalSubarrays(arr, N, K-1) and store in count1.
- Calculate the number of subarrays with a maximum not greater than K by calling function totalSubarrays(arr, N, K) and store in count2.
- Store the value of (count2 – count1) in the final value of ans and print it.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int totalSubarrays( int arr[], int n, int k)
{
int ans = 0, i = 0;
while (i < n) {
if (arr[i] > k) {
i++;
continue ;
}
int count = 0;
while (i < n && arr[i] <= k) {
i++;
count++;
}
ans += ((count * (count + 1)) / 2);
}
return ans;
}
int countSubarrays( int arr[], int n, int k)
{
int count1 = totalSubarrays(arr, n, k - 1);
int count2 = totalSubarrays(arr, n, k);
int ans = count2 - count1;
return ans;
}
int main()
{
int n = 4, k = 3;
int arr[] = { 2, 1, 3, 4 };
cout << countSubarrays(arr, n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int totalSubarrays( int arr[], int n, int k)
{
int ans = 0 , i = 0 ;
while (i < n) {
if (arr[i] > k) {
i++;
continue ;
}
int count = 0 ;
while (i < n && arr[i] <= k) {
i++;
count++;
}
ans += ((count * (count + 1 )) / 2 );
}
return ans;
}
static int countSubarrays( int arr[], int n, int k)
{
int count1 = totalSubarrays(arr, n, k - 1 );
int count2 = totalSubarrays(arr, n, k);
int ans = count2 - count1;
return ans;
}
public static void main(String[] args)
{
int n = 4 , k = 3 ;
int arr[] = { 2 , 1 , 3 , 4 };
System.out.println(countSubarrays(arr, n, k));
}
}
|
Python3
def totalSubarrays(arr, n, k):
ans = 0
i = 0
while (i < n):
if (arr[i] > k):
i + = 1
continue
count = 0
while (i < n and arr[i] < = k):
i + = 1
count + = 1
ans + = ((count * (count + 1 )) / / 2 )
return ans
def countSubarrays(arr, n, k):
count1 = totalSubarrays(arr, n, k - 1 )
count2 = totalSubarrays(arr, n, k)
ans = count2 - count1
return ans
if __name__ = = '__main__' :
n = 4
k = 3
arr = [ 2 , 1 , 3 , 4 ]
print (countSubarrays(arr, n, k))
|
C#
using System;
class GFG
{
static int totalSubarrays( int [] arr, int n, int k)
{
int ans = 0, i = 0;
while (i < n)
{
if (arr[i] > k) {
i++;
continue ;
}
int count = 0;
while (i < n && arr[i] <= k) {
i++;
count++;
}
ans += ((count * (count + 1)) / 2);
}
return ans;
}
static int countSubarrays( int [] arr, int n, int k)
{
int count1 = totalSubarrays(arr, n, k - 1);
int count2 = totalSubarrays(arr, n, k);
int ans = count2 - count1;
return ans;
}
static void Main()
{
int n = 4, k = 3;
int [] arr = { 2, 1, 3, 4 };
Console.WriteLine(countSubarrays(arr, n, k));
}
}
|
Javascript
<script>
function totalSubarrays(arr, n, k) {
let ans = 0, i = 0;
while (i < n) {
if (arr[i] > k) {
i++;
continue ;
}
let count = 0;
while (i < n && arr[i] <= k) {
i++;
count++;
}
ans += (Math.floor((count * (count + 1)) / 2));
}
return ans;
}
function countSubarrays(arr, n, k) {
let count1 = totalSubarrays(arr, n, k - 1);
let count2 = totalSubarrays(arr, n, k);
let ans = count2 - count1;
return ans;
}
let n = 4, k = 3;
let arr = [2, 1, 3, 4];
document.write(countSubarrays(arr, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...