Shortest & Easiest Brute - Force Approach with Detailed Explanation 💡 ✅
Problem Recap 📝We have an array nums of integers, and we need to find the "x-sum" for each subarray of length k. The "x-sum" is calculated by keeping the occurr
# Problem Recap 📝
*We have an array nums of integers, and we need to find the "x-sum" for each subarray of length k. The "x-sum" is calculated by keeping the occurrences of the x most frequent elements in the subarray. If two elements have the same frequency, the larger element is considered more frequent.*

# Solution Explanation 🚀
1. Prepare the result array: We need to store the results for each subarray of length k, so we initialize an empty vector ans. This will hold the x-sum for each sliding window. 🏁

**vector<int> ans;**

2. Loop through the array with a sliding window: We will process each subarray of size k and compute the "x-sum" for it. We move this window from the start of the array to the end. 🏞️

The sliding window will slide one position at a time, so we loop through the array from index 0 to nums.size() - k. So, for each starting index i:

**for (int i = 0; i <= nums.size() - k; i++) {**

3. Count element frequencies: For each subarray nums[i..i + k - 1], we need to count how often each number appears. We use an unordered_map (a hash map) to store the frequency of each element in the current window. 🔢

**unordered_map<int, int> freq;
for (int j = i; j < i + k; j++)
    freq[nums[j]]++;**

4. Sort the elements by frequency and value: Now that we have the frequency of each element in the current window, we need to sort them by two criteria:

* First by frequency (higher frequency comes first) 🏆
* If two elements have the same frequency, we sort by the value of the element (larger value comes first) 🎩

This is done by converting the unordered_map to a vector of pairs, and sorting them with a custom comparison function. 🧑‍💻

**vector<pair<int, int>> freqVec(freq.begin(), freq.end());
sort(freqVec.begin(), freqVec.end(),
     [](const pair<int, int>& a, const pair<int, int>& b) {
         if (a.second == b.second)
             return a.first > b.first; // Larger value comes first if frequencies are equal
         return a.second > b.second; // Higher frequency comes first
     });**

5. Calculate the x-sum for the current window: Now we need to calculate the "x-sum" by adding up the elements' values, but only for the top x most frequent elements. If there are fewer than x distinct elements, we sum them all up. We track how many elements we've added using the count variable. 🏅

**int sum = 0;
int count = 0;
for (auto p : freqVec) {
    if (count < x) {
        sum += p.first * p.second;  // Add element * its frequency to the sum
        count++;
    } else
        break;  // Stop once we’ve added `x` elements
}**

6. Store the result for this subarray: After calculating the x-sum for the current window, we store it in the result vector ans. 📥

**ans.push_back(sum);**

7. Return the result: After we've processed all subarrays, we return the final result array ans. This contains the x-sum for each sliding window of size k. 🎉

**return ans;**

# Visual Example
Let's consider a simple example where nums = [1, 2, 2, 3, 4], k = 3, and x = 2.

We will go through the sliding windows:

1. First window (nums[0..2] = [1, 2, 2]):

    * Frequencies: 1:1, 2:2
    * Sorted: 2:2, 1:1
    * x-sum: 2 * 2 + 1 * 1 = 5
2. Second window (nums[1..3] = [2, 2, 3]):

    * Frequencies: 2:2, 3:1
    * Sorted: 2:2, 3:1
    * x-sum: 2 * 2 + 3 * 1 = 7

3. Third window (nums[2..4] = [2, 3, 4]):

    * Frequencies: 2:1, 3:1, 4:1
    * Sorted: 4:1, 3:1, 2:1 (Sorted by value because all have same frequency)
    * x-sum: 4 * 1 + 3 * 1 = 7

Final result: [5, 7, 7] 🎯

# Conclusion 🌟
***In summary, the algorithm uses a sliding window to process each subarray of size k, counts the frequencies of elements, sorts them by frequency and value, and then calculates the x-sum based on the top x most frequent elements. The result is stored and returned as an array of x-sums for each subarray!***

# Code
```cpp []
class Solution {
public:
    vector<int> findXSum(vector<int>& nums, int k, int x) {
        vector<int> ans;
        for (int i = 0; i <= nums.size() - k; i++) {
            unordered_map<int, int> freq;
            for (int j = i; j < i + k; j++)
                freq[nums[j]]++;
            vector<pair<int, int>> freqVec(freq.begin(), freq.end());
            sort(freqVec.begin(), freqVec.end(),
                 [](const pair<int, int>& a, const pair<int, int>& b) {
                     if (a.second == b.second)
                         return a.first > b.first;
                     return a.second > b.second;
                 });
            int sum = 0;
            int count = 0;
            for (auto p : freqVec) {
                if (count < x) {
                    sum += p.first * p.second;
                    count++;
                } else
                    break;
            }
            ans.push_back(sum);
        }
        return ans;
    }
};
```
['C++']