Sliding window, Map, with explanation
IntuitionThe task requires us to calculate the sum of the x most frequent elements in each sliding window of size k from an array of numbers. To do this efficie

## Intuition

The task requires us to calculate the sum of the `x` most frequent elements in each sliding window of size `k` from an array of numbers. To do this efficiently, we need to maintain a frequency map of the elements in the current window and calculate the sum of the `x` most frequent elements as the window slides over the array.

### First Thoughts:
1. A **sliding window** approach makes sense because the problem asks us to calculate the result for each subarray of length `k`.
2. We can keep track of the frequency of elements in the window using a **frequency map** (`Map` in JavaScript).
3. After updating the map with the new element that enters the window and removing the element that leaves, we need to sort the map by frequency and sum the `x` most frequent elements.

## Approach

### Steps:
1. **Initialize the frequency map**:
   - We begin by filling the map with the frequencies of the first `k-1` elements (not including the last element for now to adjust the window in the following steps).

2. **Sliding window mechanism**:
   - For each window (from index `L` to `R`), we include the element at `R` in the frequency map.
   - Then, sort the frequency map in descending order first by frequency, and second by the value of the element itself (for ties).
   - Select the top `x` most frequent elements from the sorted list and calculate their sum, which will be the result for the current window.
   - Move the window by removing the element at index `L` from the frequency map and incrementing both `L` and `R`.

3. **Return the results**:
   - For each window, we store the calculated sum of the top `x` frequent elements in an array and return this array after processing all windows.

# Complexity
- **Time Complexity**:  
  For each window, the frequency map is sorted. If there are `m` unique elements in the window (at most `k`), the sorting cost is `O(m log m)`.  
  In the worst-case scenario, this yields an overall complexity of:  
  $$O((n - k + 1) \times k \log k) \approx O(n \times k \log k)$$

- **Space Complexity**:  
  The space is primarily used by the frequency map, which stores at most `k` unique elements.  
  Thus, the space complexity is:  
  $$O(k)$$

# Code
```typescript []
function findXSum(nums: number[], k: number, x: number): number[] {
    let answer = []
    let freqMap = new Map<number, number>()  
  
    // Add frequencies of the first k - 1 elements.
    // Since 1 <= x <= k <= nums.length, no need to check boundaries.
    // Do not include last index to make the adjustment in FOR loop down below
    for (let i = 0; i < k - 1; i++) {
        freqMap.set(nums[i], (freqMap.get(nums[i]) || 0) + 1);
    }

    for (let L = 0, R = L + k - 1; R < nums.length; L++, R++) {
        const LNum = nums[L]
        const RNum = nums[R]

        // Increase frequency of the new element at index R.
        freqMap.set(RNum, (freqMap.get(RNum) || 0) + 1);
        // Sort the frequency map entries by frequency (descending) and by number (descending) in case of ties. Return (slice) x elements
        const mostXElements = Array.from(freqMap).toSorted(([keyA, valueA], [keyB, valueB]) => {
            if (valueA !== valueB) {
                return valueB - valueA;
            }
            return keyB - keyA;
        }).slice(0, x)
        // Calculate the sum of the top x elements.
        const mostXElementsSum = mostXElements.reduce((acc, curr) => {
            return acc + curr[0] * curr[1]
        }, 0)
        answer.push(mostXElementsSum)

        // Decrease frequency of the element at index L.
        // If the resulting frequency is 0, delete it from map to reduce complexity when sorting
        freqMap.set(LNum, freqMap.get(LNum) - 1);
        if (freqMap.get(LNum) == 0) freqMap.delete(LNum);
    }

    return answer
};
```
['Hash Table', 'Sliding Window', 'TypeScript']