Brute force using Priority Queue and HashMap
Intuition\n\n# Approach\nfor every subarray define your hashmap and priority queue.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n
# Intuition\n\n# Approach\nfor every subarray define your hashmap and priority queue.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```java []\nclass Solution {\n    public int[] findXSum(int[] nums, int k, int x) {\n        int n= nums.length;\n        int[] ans = new int[n - k + 1];\n        for (int i = 0; i < n - k + 1; i++) {\n            ans[i] = getSum(nums, i, i + k - 1, x);\n        }\n        return ans;\n    }\n    public int getSum(int[] nums, int start, int end, int x) {\n        HashMap<Integer, Integer> hm = new HashMap<>();\n        for (int i = start; i <= end; i++) {\n            hm.put(nums[i], hm.getOrDefault(nums[i], 0) + 1);\n        }\n        PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>(\n            (a, b) -> (b.getValue() == a.getValue())?b.getKey()-a.getKey() :b.getValue() - a.getValue());\n        for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {\n            pq.add(entry);\n        }\n        int sum = 0;\n        while (x-- > 0 && !pq.isEmpty()) {\n            Map.Entry<Integer, Integer> e = pq.poll();\n            sum += e.getKey() * e.getValue();\n        }\n        return sum;\n    }\n}\n```
['Java']