3318. Find X-Sum of All K-Long Subarrays I
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
```python3 []
from collections import Counter

class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        result = []
        
        for i in range(len(nums) - k + 1):
            # Get the current subarray nums[i:i+k]
            subarray = nums[i:i + k]
            
            # Count frequencies of elements in the subarray
            freq = Counter(subarray)
            
            # Sort elements by frequency, and by value in case of tie
            sorted_freq = sorted(freq.items(), key=lambda item: (-item[1], -item[0]))
            
            # Get the sum of the top `x` frequent elements
            x_sum = 0
            for j in range(min(x, len(sorted_freq))):
                x_sum += sorted_freq[j][0] * sorted_freq[j][1]
            
            result.append(x_sum)
        
        return result

```
['Python3']