My Solutions
1. Use the diff array2. Use the sliding window3. Use the sliding window with less space consumption
**1. Use the diff array**
```
/**
 * Time Complexity: O(n * log(n))
 * Space Complexity: O(n)
 * where `n` is the length of the vector `nums`
 */
class Solution {
 public:
  int maxFrequency(const vector<int> &nums, const int k, const int numOperations) {
    unordered_map<int, int> num_to_count;
    for (const int num : nums) {
      ++num_to_count[num];
    }
    
    map<int, int> diffs;
    for (const auto [num, count] : num_to_count) {
      diffs[num - k] += count;
      diffs[num] += 0;   // make `num` get the chance to be considered
      diffs[num + k + 1] -= count;
    }
      
    int ret = 0;
    int count = 0;
    for (const auto [num, diff] : diffs) {
      /**
       * 1. if `num` is in `nums`
       *    the maximum frequency of `num` after performing the operations should be
       *    num_count + min(numOperations, count - num_count)
       * 2. if `num` is not in `nums`
       *    the maximum frequency of `num` after performing the operations should be
       *    min(numOperations, count)
       * by merging the above two cases, the following formula can be concluded
       * min(numOperations + num_count, count)
       *
       * the reason why just the numbers in `diffs` are considered
       * if `num` is not in `diffs`,
       * 1. (`numOperations` + `num_count`) will not be changed
       * 2. `count` will not be changed
       * so min(numOperations + num_count, count) will not be changed
       * that is `num` can be ignored safely
       */
      count += diff;
      auto itr = num_to_count.find(num);
      const int num_count = itr == num_to_count.end() ? 0 : itr->second;
      ret = max(ret, num_count + min(numOperations, count - num_count));
    }
    return ret;
  }
};
```
**2. Use the sliding window**
```
/**
 * Time Complexity: O(n * log(n))
 * Space Complexity: O(n)
 * where `n` is the length of the vector `nums`
 */
class Solution {
 public:
  int maxFrequency(const vector<int> &nums, const int k, const int numOperations) {
    /**
     * <0>: the number
     * <1>: the count of the number `<0>` in the vector `nums`
     */
    using num_count_t = array<int, 2>;
    unordered_map<int, int> num_to_count;
    for (const int num : nums) {
      ++num_to_count[num];
    }
    
    vector<num_count_t> num_counts;
    for (const auto [num, count] : num_to_count) {
      num_counts.emplace_back(num_count_t{num, count});
    }
    sort(num_counts.begin(), num_counts.end());
    const int n_num_counts = static_cast<int>(num_counts.size());
    
    vector<int> candidates;
    for (const auto [num, _] : num_counts) {
      candidates.emplace_back(num - k);
      candidates.emplace_back(num);
    }
    sort(candidates.begin(), candidates.end());
    candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
    
    const int n_candidates = static_cast<int>(candidates.size());
    int ret = 0;
    for (int start = 0, end = 0, count = 0, i = 0; i < n_candidates; ++i) {
      const int candidate = candidates[i];
      for (; end < n_num_counts && num_counts[end].front() <= candidate + k; ++end) {
        count += num_counts[end].back();
      }
      for (; start < n_num_counts && num_counts[start].front() < candidate - k; ++start) {
        count -= num_counts[start].back();
      }
      
      auto itr = num_to_count.find(candidate);
      const int num_count = itr == num_to_count.end() ? 0 : itr->second;
      ret = max(ret, num_count + min(numOperations, count - num_count));
    }
    return ret;
  }
};
```
**3. Use the sliding window with less space consumption**
```
/**
 * Time Complexity: O(n * log(n))
 * Space Complexity: O(1)
 * where `n` is the length of the vector `nums`
 */
class Solution {
 public:
  int maxFrequency(vector<int> &nums, const int k, const int numOperations) {
    const int n = static_cast<int>(nums.size());
    sort(nums.begin(), nums.end());
    int ret = 0;
    for (int start = 0, end = 0, i = 0; i < n; ) {
      int next_i = i + 1;
      for (; next_i < n && nums[i] == nums[next_i]; ++next_i) {
      }
      const int num_count = next_i - i;
      for (; start < n && nums[start] < nums[i] - k; ++start) {
      }
      for (; end < n && nums[end] <= nums[i] + k; ++end) {
      }
      ret = max(ret, num_count + min(numOperations, end - start - num_count));
      i = next_i;
    }
    
    if (ret >= numOperations) {
      return ret;
    }
    
    for (int start = 0, end = 0; end < n; ) {
      int next_end = end + 1;
      for (; next_end < n && nums[end] == nums[next_end]; ++next_end) {
      }
      for (; start < n && nums[start] < nums[end] - (k << 1); ++start) {
      }
      ret = max(ret, min(numOperations, next_end - start));
      end = next_end;
    }
    return ret;
  }
};
```
['C++']