💡✅🔥 Sliding window with map + detailed explanation
Intuition\nThis task takes some time to interpret the requirements but basically it\'s easy.\n\n# Approach\n- Consider subarrays with sliding window within i..(
# Intuition\nThis task takes some time to interpret the requirements but basically it\'s easy.\n\n# Approach\n- Consider subarrays with sliding window within $$i..(i+k-1)$$ indexes\n- For each subarray:\n    - calculate numbers occurance\n    - sort by occurance, then by number value\n    - make product-sum within top `x` occurances\n\n# Complexity\n- Time complexity:\n$$O(n * k)$$ for sliding window\n$$O(n * logn)$$ for sorting occurances\n$$O(n * x)$$ for summarizing the result\n\nOverall: $$O(n * (k + logn + x))$$\n\n\n- Space complexity:\n$$O(n)$$ for occurances\n\n# 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    const ans = [];\n\n    for (let i = 0; i < nums.length - k + 1; i++) {\n        const map = {};\n\n        // counting occurances\n        for (let j = i; j <= i + k - 1 && j < nums.length; j++) {\n            if (!map[nums[j]]) {\n                map[nums[j]] = 0;\n            }\n\n            map[nums[j]] += 1;\n        }\n\n        // sort\n        const entries = Object.entries(map);\n\n        entries.sort((e1,e2) => {\n            if (e2[1] !== e1[1]) {\n                return e2[1] - e1[1];\n            }\n\n            return e2[0] - e1[0];\n        });\n\n        // calculating top x sum\n        let sum = 0;\n\n        for (let p = 0; p < x && p < entries.length; p++) {\n            sum += +entries[p][0] * +entries[p][1];\n        }\n\n        ans.push(sum);\n    }\n\n    return ans;\n};\n```
['Array', 'Hash Table', 'Sliding Window', 'Sorting', 'JavaScript']