It is NOT an easy problem to solve. Using Sliding window, Priority queue
Code\njavascript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} x\n * @return {number[]}\n */\nvar findXSum = function (nums, k, x
# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} x\n * @return {number[]}\n */\nvar findXSum = function (nums, k, x) {\n    // Initialize values\n    // Answer, array is sliding window,\n    // memo is where the frequency of occurrence of numbers is stored\n    // compare is a function that compares the frequency of\n    // occurrence of numbers in the sliding window. If the frequency\n    // is the same, it will compare which number is larger.\n    let answer = new Array(nums.length - k + 1).fill(0)\n    let array\n    let memo = {}\n    let compare = ((a, b) => {\n        if (memo[a] != memo[b]) {\n            return memo[b] - memo[a]\n        }\n        return b - a\n    })\n\n\n    // Fill each element into the answer array\n    for (let i = 0; i < answer.length; i++) {\n\n        // [START] sliding window\n        if (i == 0) {\n            array = nums.slice(0, k)\n            for (let ar of array) {\n                memo[ar] = (memo[ar] || 0) + 1\n            }\n        } else {\n            let elDel = array.shift()\n            let elInsert = nums[i + k - 1]\n            array.push(elInsert)\n            memo[elDel] -= 1\n            memo[elInsert] = (memo[elInsert] || 0) + 1\n        }\n        // [END] sliding window\n\n        // use PriorityQueue to sort\n        let priority = new PriorityQueue({ compare })\n        for (let key in memo) {\n            priority.enqueue(key)\n        }\n\n        // Use PriorityQueue to get the most frequent numbers from\n        // largest to smallest for calculation\n        let ans = 0\n        for (let j = 0; j < x; j++) {\n            let el = priority.dequeue()\n            if (el) {\n                ans += memo[el] * el\n            }\n        }\n        answer[i] = ans\n    }\n    return answer\n};\n```
['JavaScript']