Sliding window
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
 public:
  std::vector<int> findXSum(const std::vector<int>& nums, int k, int x) const {
    std::vector<int> result;
    size_t left = 0;
    size_t right = k;
    std::unordered_map<int, int> num2freq;
    std::map<int, std::set<int, std::greater<int>>, std::greater<>> freq2num;
    for (size_t i = left; i < right; ++i) {
      int freq = num2freq[nums[i]]++;
      if (freq > 0) {
        freq2num[freq].erase(nums[i]);
        if (freq2num[freq].empty()) {
          freq2num.erase(freq);
        }
      }
      freq2num[freq + 1].insert(nums[i]);
    }

    int cnt = 0;
    int sum = 0;

    // X-sum
    for (auto it = freq2num.begin(); it != freq2num.end() && cnt < x; ++it) {
      for (auto it_inner = it->second.begin();
           it_inner != it->second.end() && cnt < x; ++it_inner) {
        sum += *it_inner * num2freq[*it_inner];
        ++cnt;
      }
    }
    result.push_back(sum);
    sum = 0;
    cnt = 0;

    while (right < nums.size()) {
      // Delete nums[left], update the freq2num and num2freq for nums[left]
      int freq = num2freq[nums[left]]--;
      freq2num[freq].erase(nums[left]);
      if (freq2num.empty()) {
        freq2num.erase(freq);
      }
      if (freq > 1) {
        freq2num[freq - 1].insert(nums[left]);
      }
      // Insert nums[right], update the freq2num and num2freq for nums[right]
      freq = num2freq[nums[right]]++;
      if (freq > 0) {
        freq2num[freq].erase(nums[right]);
        if (freq2num[freq].empty()) {
          freq2num.erase(freq);
        }
      }
      freq2num[freq + 1].insert(nums[right]);

      for (auto it = freq2num.begin(); it != freq2num.end() && cnt < x; ++it) {
        for (auto it_inner = it->second.begin();
             it_inner != it->second.end() && cnt < x; ++it_inner) {
          sum += *it_inner * num2freq[*it_inner];
          ++cnt;
        }
      }
      result.push_back(sum);
      sum = 0;
      cnt = 0;

      ++left;
      ++right;
    }
    return result;
  }
};
```
['C++']