PriorityQueue (Min heap) + Sliding Window Solution in Java
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n * log(x))\n\n- Space complexity:\nO(k)\n\n# Code\n```java []\nclass Solution {\n    public int[] findXSum(int[] nums, int k, int x) {\n        Map<Integer, Integer> count = new HashMap<>();\n        PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> {\n            if (count.get(a) == count.get(b)) {\n                return a - b;\n            } else {\n                return count.get(a) - count.get(b);\n            }\n        });\n        int n = nums.length;\n        int[] ans = new int[n - k + 1];\n        for (int i = 0; i < k; i++) {\n            count.put(nums[i], count.getOrDefault(nums[i], 0) + 1);\n        }\n        for (int key : count.keySet()) {\n            minHeap.offer(key);\n            if (minHeap.size() > x) {\n                minHeap.poll();\n            }\n        }\n        ans[0] = calculateXSum(count, minHeap);\n        for (int i = k; i < n; i++) {\n            count.put(nums[i], count.getOrDefault(nums[i], 0) + 1);\n            int outgoing = nums[i - k];\n            count.put(outgoing, count.get(outgoing) - 1);\n            if (count.get(outgoing) == 0) {\n                count.remove(outgoing);\n            }\n            minHeap.clear();\n            for (int key : count.keySet()) {\n                minHeap.offer(key);\n                if (minHeap.size() > x) {\n                    minHeap.poll();\n                }\n            }\n            ans[i - k + 1] = calculateXSum(count, minHeap);\n        }\n\n        return ans;\n    }\n\n    public int calculateXSum(Map<Integer, Integer> count, PriorityQueue<Integer> minHeap) {\n        int sum = 0;\n        for (int num : minHeap) {\n            sum += num * count.get(num);\n        }\n        return sum;\n    }\n}\n```
['Sliding Window', 'Heap (Priority Queue)', 'Java']