3318. Find X-Sum of All K-Long Subarrays I
IntuitionWhen approaching this problem, I was reminded of the sliding window technique commonly used to solve the maximum sum of a subarray problem. In this con
# Intuition
When approaching this problem, I was reminded of the sliding window technique commonly used to solve the maximum sum of a subarray problem. In this context, we need to keep track of the frequency of elements within each sliding window and prioritize them to find the top x most frequent elements.

To achieve this, I used a custom comparator to handle the frequency and value of elements. By employing a priority queue, I was able to efficiently manage the elements based on their frequencies and compute the x-sum for each window.

# Approach
1.Custom Comparator Class:

Define a class Freq that implements Comparable<Freq> to store the value and frequency of each element.

Implement the compareTo method to sort the elements based on their frequency. If frequencies are equal, sort them by their value in reverse order.

2.Sliding Window Technique:

Initialize a frequency array to keep track of the count of each element within the current window.

Use a priority queue to manage and retrieve the elements with the highest frequencies in each window.

# Complexity
- Time complexity: O(nlogk) 

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

# Code
```java []
class Solution {

    public class Freq implements Comparable<Freq> {
        int val;
        int freq;

        public Freq(int val, int freq) {
            this.val = val;
            this.freq = freq;
        }

        @Override
        public int compareTo(Freq v2) {
            if (this.freq == v2.freq) {
                // If frequencies are the same, return the comparison of values in reverse order
                return Integer.compare(v2.val, this.val); 
            } 
            else {
                // Compare frequencies normally (higher frequency comes first)
                return Integer.compare(v2.freq, this.freq);
            }
        }
    }

    public int[] findXSum(int[] nums, int k, int x) {
    int n = nums.length;

    int[] freq = new int[51];  // Frequency array (1 <= nums[i] <= 50)
    int[] result = new int[n - k + 1]; // Result array

    PriorityQueue<Freq> pq = new PriorityQueue<>();

    // Initializing the first window
    for (int i = 0; i < k; i++) {
        freq[nums[i]]++;
    }

    // Sliding window processing
    for (int i = 0; i <= n - k; i++) {
        if (i > 0) {
            // Update frequency for sliding window
            freq[nums[i - 1]]--; // Element leaving the window
            freq[nums[i + k - 1]]++; // Element entering the window
        }

        // Rebuild the priority queue based on current frequencies
         pq.clear();
        for (int j = 1; j <= 50; j++) {
            if (freq[j] > 0) {
                pq.add(new Freq(j, freq[j]));
            }
        }

        // Compute x-sum
        int sum = 0, count = 0;
        while (!pq.isEmpty() && count < x) {
            Freq top = pq.poll();
            sum += top.val * top.freq;
            count++;
        }

        result[i] = sum;
    }

    return result;
    }
}

```
['Heap (Priority Queue)', 'Java']