Java&JS&TS Solution (JW)
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
```java []
public int[] findXSum(int[] nums, int k, int x) {
    int n = nums.length;
    int[] result = new int[n - k + 1];
    
    Map<Integer, Integer> freq = new HashMap<>();
    
    for (int i = 0; i < k; i++) {
        freq.put(nums[i], freq.getOrDefault(nums[i], 0) + 1);
    }
    result[0] = computeXSum(freq, x);
    
    for (int i = k; i < n; i++) {
        // remove leftmost
        int left = nums[i - k];
        freq.put(left, freq.get(left) - 1);
        if (freq.get(left) == 0) freq.remove(left);
        
        // add new right
        int right = nums[i];
        freq.put(right, freq.getOrDefault(right, 0) + 1);
        
        result[i - k + 1] = computeXSum(freq, x);
    }
    
    return result;
}

private int computeXSum(Map<Integer, Integer> freq, int x) {
    List<int[]> freqList = new ArrayList<>();
    for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
        freqList.add(new int[]{entry.getKey(), entry.getValue()});
    }

    // Sort by frequency DESC, then value DESC
    freqList.sort((a, b) -> {
        if (a[1] != b[1]) return Integer.compare(b[1], a[1]);
        return Integer.compare(b[0], a[0]);
    });

    Set<Integer> topX = new HashSet<>();
    for (int i = 0; i < Math.min(x, freqList.size()); i++) {
        topX.add(freqList.get(i)[0]);
    }

    // Sum up only top x frequent values
    int sum = 0;
    for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
        if (topX.contains(entry.getKey())) {
            sum += entry.getKey() * entry.getValue();
        }
    }
    return sum;
}
```
```javascript []
var findXSum = function(nums, k, x) {
    const n = nums.length;
    const result = [];
    const freq = new Map();

    // Initialize frequency map with first k elements
    for (let i = 0; i < k; i++) {
        freq.set(nums[i], (freq.get(nums[i]) || 0) + 1);
    }

    result.push(computeXSum(freq, x));

    for (let i = k; i < n; i++) {
        // Remove element going out of the window
        const out = nums[i - k];
        freq.set(out, freq.get(out) - 1);
        if (freq.get(out) === 0) {
            freq.delete(out);
        }

        // Add element coming into the window
        const inVal = nums[i];
        freq.set(inVal, (freq.get(inVal) || 0) + 1);

        result.push(computeXSum(freq, x));
    }

    return result;
};

function computeXSum(freqMap, x) {
    const freqList = [];

    // Convert freqMap to list of [value, count]
    for (let [val, count] of freqMap.entries()) {
        freqList.push([val, count]);
    }

    // Sort by frequency DESC, then value DESC
    freqList.sort((a, b) => {
        if (b[1] !== a[1]) return b[1] - a[1];
        return b[0] - a[0];
    });

    // Select top x values
    const topX = new Set();
    for (let i = 0; i < Math.min(x, freqList.length); i++) {
        topX.add(freqList[i][0]);
    }

    // Sum up values from top x
    let sum = 0;
    for (let [val, count] of freqMap.entries()) {
        if (topX.has(val)) {
            sum += val * count;
        }
    }

    return sum;
}
```
```typescript []
function findXSum(nums: number[], k: number, x: number): number[] {
    const n = nums.length;
    const result: number[] = [];
    const freq: Map<number, number> = new Map();

    // Initialize frequency map with first k elements
    for (let i = 0; i < k; i++) {
        freq.set(nums[i], (freq.get(nums[i]) || 0) + 1);
    }

    result.push(computeXSum(freq, x));

    // Slide the window
    for (let i = k; i < n; i++) {
        const outVal = nums[i - k];
        const outCount = freq.get(outVal)! - 1;
        if (outCount === 0) {
            freq.delete(outVal);
        } else {
            freq.set(outVal, outCount);
        }

        const inVal = nums[i];
        freq.set(inVal, (freq.get(inVal) || 0) + 1);

        result.push(computeXSum(freq, x));
    }

    return result;
};

function computeXSum(freqMap: Map<number, number>, x: number): number {
    const freqList: [number, number][] = [];

    // Convert map to list of [value, frequency]
    for (const [val, count] of freqMap.entries()) {
        freqList.push([val, count]);
    }

    // Sort by frequency descending, then value descending
    freqList.sort((a, b) => {
        if (b[1] !== a[1]) return b[1] - a[1];
        return b[0] - a[0];
    });

    // Pick top x values
    const topX = new Set<number>();
    for (let i = 0; i < Math.min(x, freqList.length); i++) {
        topX.add(freqList[i][0]);
    }

    // Compute sum of top x frequent values
    let sum = 0;
    for (const [val, count] of freqMap.entries()) {
        if (topX.has(val)) {
            sum += val * count;
        }
    }

    return sum;
}
```
['Array', 'Hash Table', 'Sliding Window', 'Heap (Priority Queue)', 'Java', 'TypeScript', 'JavaScript']