Count of subarrays whose maximum element is greater than k in C++

C++Server Side ProgrammingProgramming

We are given an array arr[] containing integer elements and a variable k . The goal is to find the count of subarrays of arr[] that have greatest/maximum element more that k. If the array is [1,2,3] and k is 1. Then possible subarrays are [1], [2], [3], [1,2], [2,3], [1,2,3]. The subarrays with maximum element > 1 are [2], [3], [1,2], [2,3], [1,2,3]. So the count is 5.

Let us understand with examples

Input − arr[] = {1,2,5,3 } k=3

Output − Count of subarrays whose maximum element is greater than k are − 6

Explanation − All possible subarrays are [1], [2], [5], [3], [1,2], [2,5], [5,3], [1,2,5], [2,5,3], [1,2,5,3]. Out of these arrays with maximum elements greater than 3 are the ones that have 5 in it. Those are −

[5], [2,5], [5,3], [1,2,5], [2,5,3], [1,2,5,3].

Total 6 subarrays.

Input − arr[] = {1,2,3,4,5 } k=4

Output− Count of subarrays whose maximum element is greater than k are − 5

Explanation − Only elements greater than 4 is 5. Subarrays containing 5 will be −

[5], [4,5], [3,4,5], [2,3,4,5], [1,2,3,4,5].

Total 5 subarrays.

Approach used in the below program is as follows

In this approach we know that total number of subarrays of array with n elements is n*(n+1)/2.

We will now look for subarrays that have elements < k. For this we skip elements >k and count the length of subarrays with all elements less than k. For each length l, each subarray can make l*(l+1)/2 subarrays. Add this value for each such subarray to say X. Now at last we subtract this value X from n*(n+1)/2 to get the desired result.

  • Take integer array arr[] and variable k as input.

  • Function maximum_k(int arr[], int size, int k) takes array, k and array's length and returns the count of subarrays whose maximum element is greater than k

  • Take the initial count as 0.

  • Using while loop, traverse array from index i=0 to i<size.

  • For each element if arr[i]>k skip it using a continue statement.

  • Else start counting the length of the subarray using an inner while loop.

  • If arr[i]<k and i<size. Increment i and temp (length of subarray with all elements < k).

  • At the end of the inner while we have the length of the current subarray as temp.

    Calculate temp*(temp+1)/2 and add to count.

  • Do this for all such subarrays.

  • At the end of outer while. We have variable count as the number of all subarrays with elements < k.

  • Update count by subtracting this count from number of all possible subarrays of arr[], that is size*(size-1)/2.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int maximum_k(int arr[], int size, int k){
   int count = 0;
   int i = 0;
   while (i < size){
      if (arr[i] > k){
         i++;
         continue;
      }
      int temp = 0;
      while (i < size && arr[i] <= k){
         i++;
         temp++;
      }
      int temp_2 = temp * (temp + 1);
      count = count + temp_2 / 2;
   }
   count = (size * (size + 1) / 2 - count);
   return count;
}
int main(){
   int arr[] = { 4, 1, 2, 7, 8, 3 };
   int k = 5;
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<"Count of subarrays whose maximum element is greater than k are: "<<maximum_k(arr, size, k);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of subarrays whose maximum element is greater than k are: 14
raja
Published on 02-Dec-2020 12:10:04
Advertisements