Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
887 B
0
Indexable
Never
#include <vector>

using namespace std;

vector<int> getMeanRankCount(vector<int> rank) {
  // Initialize the result array.
  vector<int> result(rank.size() + 1, 0);

  // Iterate over all possible mean values.
  for (int mean = 1; mean <= rank.size(); mean++) {
    // Initialize the count of groups with mean value equal to mean.
    int count = 0;

    // Iterate over all subarrays of the given array.
    for (int i = 0; i < rank.size(); i++) {
      int sum = 0;
      int n = 1;

      // Iterate over the elements of the subarray.
      for (int j = i; j < rank.size() && rank[j] <= mean; j++) {
        sum += rank[j];
        n++;
      }

      // If the mean value of the subarray is equal to mean, increment the count.
      if (sum / n == mean) {
        count++;
      }
    }

    // Store the count in the result array.
    result[mean] = count;
  }

  return result;
}