Java Easy Solution Explained
Code
# Code
```java []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        int[] result = new int[nums.length - k + 1];
        for (int i = 0; i < result.length; i++) {
            result[i] = findSum(nums, i, i + k, x);
        }
        return result;
    }

    private int findSum(int[] nums, int i, int j, int x) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int k = i; k < j; k++) {
            map.put(nums[k], map.getOrDefault(nums[k], 0) + 1);
        }

        PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> a.freq == b.freq ? a.val - b.val : a.freq - b.freq);
        for (int key : map.keySet()) {
            pq.offer(new Pair(key, map.get(key)));
            if (pq.size() > x)
                pq.poll();
        }

        int sum = 0;
        while (pq.size() > 0) {
            Pair p = pq.poll();
            sum += p.freq * p.val;
        }

        return sum;
    }

    class Pair {
        int val, freq;

        public Pair(int v, int f) {
            val = v;
            freq = f;
        }
    }
}
```
``` Java-explained []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        // Initialize the result array with size n - k + 1
        int[] result = new int[nums.length - k + 1];

        // Iterate over each sliding window of size k
        for (int i = 0; i < result.length; i++) {
            // Compute x-sum for the subarray nums[i..i+k-1]
            result[i] = findSum(nums, i, i + k, x);
        }

        return result;
    }

    private int findSum(int[] nums, int i, int j, int x) {
        // Create a map to count the frequency of elements in the subarray
        Map<Integer, Integer> map = new HashMap<>();
        for (int k = i; k < j; k++) {
            map.put(nums[k], map.getOrDefault(nums[k], 0) + 1);
        }

        // Use a min-heap (priority queue) to keep track of top x most frequent elements
        // If frequencies are equal, the element with the smaller value has lower priority
        PriorityQueue<Pair> pq = new PriorityQueue<>(
            (a, b) -> a.freq == b.freq ? a.val - b.val : a.freq - b.freq
        );

        // Iterate through the frequency map
        for (int key : map.keySet()) {
            pq.offer(new Pair(key, map.get(key))); // Push each element and its frequency

            // Keep only top x frequent elements in the heap
            if (pq.size() > x)
                pq.poll(); // Remove the least frequent (or smaller valued if tie)
        }

        // Sum up the total frequency-weighted values from the top x elements
        int sum = 0;
        while (!pq.isEmpty()) {
            Pair p = pq.poll();
            sum += p.freq * p.val; // Multiply value by frequency
        }

        return sum;
    }

    // Helper class to store element value and its frequency
    class Pair {
        int val, freq;

        public Pair(int v, int f) {
            val = v;
            freq = f;
        }
    }
}
```
['Java']