Basic solution for beginners!!
Intuition\nBreak Down the Problem into Subarrays \uD83D\uDD04:\n\nWe slide through the array nums using a window of size k. This allows us to look at all possib
# Intuition\nBreak Down the Problem into Subarrays \uD83D\uDD04:\n\nWe slide through the array nums using a window of size k. This allows us to look at all possible contiguous subarrays of length k.\nFor each of these subarrays, we need to determine the most frequent elements and sum up the top x of them.\nTrack Frequency of Elements in Each Subarray \uD83D\uDCCA:\n\nFor each subarray, we keep track of how often each number appears using a HashMap.\nThis helps us know the frequency of each element quickly.\nUse a Priority Queue (Max-Heap) for the Most Frequent Elements \uD83C\uDF1F:\n\nOnce we have the frequency of each element in the current subarray, we want to find the top x most frequent ones.\nWe use a max-priority queue (PriorityQueue) to efficiently sort and extract these elements based on:\nFrequency: Elements with higher frequency come first.\nValue: If two elements have the same frequency, the larger value is prioritized.\nCompute the Weighted Sum of the Top x Elements \u2795:\n\nWe extract the top x elements from the priority queue, multiply each by its frequency (to consider all its occurrences), and sum them up.\nThis sum represents the desired value for the current subarray.\nStore and Return the Results \uD83D\uDCDD:\n\nWe store the computed sum for each subarray in the result array res, and once all subarrays are processed, we return the array.\n\n# Approach\nEfficient Frequency Counting: Using a HashMap ensures that we quickly get the frequency of each element in the subarray.\nPriority Queue Optimization: By using a max-heap, we efficiently get the top x elements without needing to fully sort all entries, making the solution faster.\nSliding Window: The approach of iterating through each possible subarray ensures that we explore all parts of the array, giving a complete and accurate result.\n\n# Complexity\nTime Complexity: O(n * (k + x log k))\nExplanation: For each of the n - k + 1 subarrays, we perform operations that take O(k + x log k) time.\n\nSpace Complexity: O(n + k)\nExplanation: We store the result in an array of size O(n) and use auxiliary data structures (HashMap and PriorityQueue) that take up O(k) space per iteration.\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[] res = new int[n-k+1];\n        for(int i=0;i<=n-k;i++){\n            HashMap<Integer,Integer> map = new HashMap<>();\n\n            for(int j=i;j<i+k;j++){\n                map.put(nums[j] , map.getOrDefault(nums[j],0)+1);\n            }\n\n            PriorityQueue<Map.Entry<Integer,Integer>> pq = new PriorityQueue<>((a,b)->{\n                if(a.getValue() != b.getValue()){\n                    return b.getValue() - a.getValue();\n                }\n                return b.getKey() - a.getKey();\n            });\n\n            pq.addAll(map.entrySet());\n            \n\n            int sum = 0;\n            for(int j=0;j<x && !pq.isEmpty();j++){\n                Map.Entry<Integer,Integer> e = pq.poll();\n                sum += e.getKey()*e.getValue();\n            }\n            res[i] = sum;\n        }\n        return res;\n    }\n}\n```
['Hash Table', 'Heap (Priority Queue)']