Count K-length subarrays whose average exceeds the median of the given array


The expression "K-length subarrays" pertains to successive subarrays having precisely K elements. Grasping and working with subarrays is crucial for resolving various issues in fields such as dynamic programming, computational geometry, and data analysis.

Another vital concept in array manipulation and statistics is the median. The median of an array represents the middle value when the elements are sorted in ascending order. In the case of an even number of elements, the median is the average of the two central values. The median constitutes a durable measure of central tendency, as it is less vulnerable to extreme values or outliers as compared to the mean.

This article endeavors to examine the challenge of determining the number of K-length subarrays whose average exceeds the median of a given array. By comprehending the relationship between the average and median of a dataset, we can delve into this challenge and develop efficient techniques for resolving it. Join us as we dissect the problem statement, examine key concepts, and work through algorithms to count the desired K-length subarrays in an array with efficiency.

Syntax

Sorts the elements in the array in ascending order.

sort(begin(array), end(array))

Declares a vector of integers.

vector vec

Declares an integer array

int arr[]

Basic for loop syntax in C++.

for(int i=0; i<size; ++i)

Algorithm of Source Code

  • Read the input array and its size.

  • Calculate the median of the given array.

  • For each K-length subarray, calculate the average.

  • Compare the average with the median.

  • Count the subarrays whose average exceeds the median.

Approach 1: Brute Force

The approach 1 constitutes a straightforward solution to the challenge of determining the number of K-length subarrays with an average surpassing the median of the specified array. Initially, the input array is sorted and the median is calculated. Subsequently, the program traverses all feasible K-length subarrays and computes their averages by aggregating their components. Should the average of a subarray surpass the median, the count is augmented. Finally, the code returns the quantity of such subarrays.

Algorithm

  • Calculate the median of the given array.

  • Iterate through all possible K-length subarrays.

  • Calculate the average of each subarray.

  • Increment the count if the subarray's average is greater than the median.

Example-1

The below code follows the brute force approach as previously mentioned in the article. It first sorts the input array and calculates the median. Then, it iterates through all possible K-length subarrays and calculates their averages by summing their elements. If the average of a subarray is greater than the median, the count is incremented. Finally, the code returns the count of such subarrays.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int countSubarrays(vector<int> &arr, int n, int k) {
   int count = 0;
   double median;
   sort(arr.begin(), arr.end());
   median = (n % 2 == 0) ? (arr[n/2 - 1] + arr[n/2]) / 2.0 : arr[n/2];

   for (int i = 0; i <= n - k; i++) {
      double sum = 0;
      for (int j = i; j < i + k; j++) {
         sum += arr[j];
      }
      if (sum / k > median) {
         count++;
      }
   }
   return count;
}

int main() {
   vector<int> arr = {1, 5, 6, 7, 9};
   int n = arr.size();
   int k = 3;
   int result = countSubarrays(arr, n, k);
   cout << "Number of K-length subarrays with average exceeding median: " << result << endl;
   return 0;
}

Output

Number of K-length subarrays with average exceeding median: 1

Approach 2: Optimized Approach

The approach 2 constitutes a refined solution to the problem of determining the quantity of K-length subarrays with a mean surpassing the median of the specified array. It commences by sorting the input array and computing the median. Next, it calculates the prefix sum array, which is utilized to determine the sum of each K-length subarray. The algorithm iterates through all possible K-length subarrays, computes their average using the prefix sum array, and compares it with the median.

Should the average of a subarray exceed the median, the count is raised. Eventually, the program returns the number of such subarrays. This approach is more efficient than the first one, as it utilizes the prefix sum array to compute the sum of each K-length subarray, leading to a decreased elapsed time intricacy.

Algorithm

  • Calculate the median of the given array.

  • Compute the prefix sum array.

  • Iterate through all possible K-length subarrays.

  • Calculate the average using the prefix sum array.

  • Increment the count if the subarray's average is greater than the median.

Example-2

The algorithm adheres to the previously described optimal method. It leverages a prefix sum array to calculate the aggregate of each K-length subset with alacrity. Upon sorting the input sequence and determining the median, the prefix sum is computed. Then, the program traverses all K-length subsets, computes their mean with the prefix sum array, and compares it against the median. If the mean surpasses the median, the tally is increased. In conclusion, the code returns the number of such subsets.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int countSubarrays(vector<int> &arr, int n, int k) {
   int count = 0;
   double median;
   sort(arr.begin(), arr.end());
   median = (n % 2 == 0) ? (arr[n/2 - 1] + arr[n/2]) / 2.0 : arr[n/2];

   vector<int> prefix_sum(n);
   prefix_sum[0] = arr[0];
   for (int i = 1; i < n; i++) {
      prefix_sum[i] = prefix_sum[i - 1] + arr[i];
   }

   for (int i = 0; i <= n - k; i++) {
      double sum = (i == 0) ? prefix_sum[i + k - 1] : prefix_sum[i + k - 1] - prefix_sum[i - 1];
      if (sum / k > median) {
         count++;
      }
   }
   return count;
}

int main() {
   vector<int> arr = {1, 5, 6, 7, 9};
   int n = arr.size();
   int k = 3;
   int result = countSubarrays(arr, n, k);
   cout << "Number of K-length subarrays with average exceeding median: " << result << endl;
   return 0;
}

Output

Number of K-length subarrays with average exceeding median: 1

Conclusion

In this article, we discussed two approaches to count K-length subarrays whose average exceeds the median of the given array using C++. The first approach is a brute force method, which iterates through all possible K-length subarrays and calculates their averages. The second approach is an optimized method that uses a prefix sum array to calculate the averages more efficiently. Both codes have been provided and can be executed to find the required count of subarrays.

Updated on: 21-Jul-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements