Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks to calculate the "x-sum" of each k-length subarray. The x-sum is deriv
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks to calculate the "x-sum" of each k-length subarray. The x-sum is derived by keeping the most frequent elements, considering the higher value in case of ties.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Iterate through each subarray of length k.\n2. For each subarray:\n- Count the frequency of each element.\n- Use a max heap (priority queue) to keep the top x most frequent elements.\n- Calculate the sum of those elements.\n3. Store the x-sum result for each subarray in the final output array.\n# Complexity\n- Time complexity:\n- O(n * k * log k) due to subarray creation and heap operations.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(k) for the frequency map and heap.\n<!-- Add your space complexity here, e.g. $$O(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[] result = new int[n - k + 1];\n\n        for (int i = 0; i <= n - k; i++) {\n            int[] subarray = Arrays.copyOfRange(nums, i, i + k);\n            result[i] = calculateXSum(subarray, x);\n        }\n\n        return result;\n    }\n\n    private int calculateXSum(int[] subarray, int x) {\n        Map<Integer, Integer> frequencyMap = new HashMap<>();\n        for (int num : subarray) {\n            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);\n        }\n\n        PriorityQueue<int[]> maxHeap = new PriorityQueue<>(\n            (a, b) -> a[1] != b[1] ? b[1] - a[1] : b[0] - a[0]\n        );\n\n        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {\n            maxHeap.offer(new int[] {entry.getKey(), entry.getValue()});\n        }\n\n        int sum = 0;\n        for (int i = 0; i < x && !maxHeap.isEmpty(); i++) {\n            int[] top = maxHeap.poll();\n            sum += top[0] * top[1];\n        }\n        return sum;\n    }\n}\n```
['Array', 'Hash Table', 'Java']