C++ | Easy approach | Intuitive
Complexity

Time complexity:
O(nlogn)


Space complexity:
O(n)

Code
# Complexity
- Time complexity:
O(nlogn)

- Space complexity:
O(n)

# Code
```cpp []
class Solution {
public:

    int calculateXSum(vector<int>& nums, int x){
        unordered_map<int,int> frequencyCount;
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> minHeap;
        int sum=0;

        for(int i=0;i<nums.size();i++){
           frequencyCount[nums[i]]++;
        }

        for(auto i: frequencyCount){
            minHeap.push({i.second, i.first});
            if(minHeap.size() > x) {
                minHeap.pop();
            }
        }

        while(!minHeap.empty()){
            pair<int,int> item = minHeap.top();
            sum = (item.first*item.second) + sum;
            minHeap.pop();
        }
        return sum;
    }

    vector<int> findXSum(vector<int>& nums, int k, int x) {
        vector<int> result;
        int s = nums.size();
  
        for(int i=0;i+k-1<s;i++){
            vector<int> subArray;
            for(int j=i;j<i+k;j++){
               subArray.push_back(nums[j]);
            }     
            result.push_back(calculateXSum(subArray,x));   
        }
        
        return result;
    }
};
```
['C++']