N*x*LogK solution , Sliding Window  + TreeSet solution 
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- From the description of the problem we can understand we need to have on demand sorting as at each instant and at each window we will want to have the elements sorted based on their occurrences .Hence for each subarray we treat it as a window and at each window the TreeSet maintains the sort order by their frequencies and we can just interate the top x elements   -->

# Approach
<!-- 
    For the window 0 to k -1 , 
    use the hashMap to make the element -> frequencyMap 

    Then for the rest of the subarrays 
    from window 
    start = i 
    end = i + k - 1 

    remove the identity ( element and its old frequency )
    of nums[start]

    and then add the identity (element + its new frequency )

    Similarly do the same for the nums[end]


    P.S wondering if their is a way , we can have ondemand ans[i]    update    from ans[i - 1]    . Because at max frequency of two elements are changing . 
    . -->

# Complexity
- Time complexity:
<!-- O(N*LogK)
    N = Size Of the Array 
    K = Size of the Subarray  -->

- Space complexity:
<!-- K = TreeSet Size at any instance
     N- k + 1 = Answer Array 
     Overall Size = N + 1 -->

# Code
```java []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        

        class Pair{

            int first;
            int second;

            public Pair(int first , int second ) {
                this.first = first;
                this.second = second;
            }
        }

        


        HashMap<Integer , Integer > freq = new HashMap<>();
        
        TreeSet<Pair> treeSet = new TreeSet<>( (a , b ) -> {

            if(b.second == a.second ) {
                return b.first - a.first;
            }
            return b.second - a.second;
        });


        int n = nums.length;

        int[]  ans = new int[n - k + 1 ];

        for(int i = 0 ; i < n - k + 1 ; i ++ ) {

            ans[i] = 0;
        }

        for(int i = 0 ; i < k ; i ++  ) {
            freq.put(nums[i] , freq.getOrDefault(nums[i] , 0) + 1);
        }

        for(Map.Entry<Integer, Integer> entry : freq.entrySet()) {

            treeSet.add(new Pair(entry.getKey() , entry.getValue()));
        }

        

        Iterator<Pair> iterator = treeSet.iterator();
        int cnt = 0 ;
        while(cnt < x && iterator.hasNext()) {
            Pair p = iterator.next();
            ans[0] += p.first * p.second;
            //System.out.println(p.first + "," + p.second);
            cnt++;
        }

        

        for(int i = 1 ; i < n ; i ++ ) {

            int start = i ;
            int end = i + k - 1 ;

            if(end >= n ) {
                break;
            }

            // removing the start - 1 th element from the window
            int oldFreq = freq.getOrDefault(nums[start - 1] , 0);
            Pair p = new Pair(nums[i - 1] , oldFreq); // already existing node to be removed 
            treeSet.remove(p); // removing it 
            freq.put(nums[i - 1] , oldFreq - 1); // subtracting 1 freq as it is removed from the node 
            treeSet.add(new Pair(nums[i - 1] , oldFreq - 1)); // adding a new node to the tree 
            

            // add the new element , that is the end element to the window 
            int newFreq = freq.getOrDefault(nums[end] , 0); // getting the existing freq from the node 
            treeSet.remove(new Pair(nums[end] , newFreq )); // removing it from the TreeSet
            freq.put(nums[end] , newFreq + 1); // adding freq to the hashmap 
            treeSet.add(new Pair(nums[end] , newFreq + 1 )); // adding new node to the TreeSet

            //System.out.println("\n");
            cnt = 0 ;
            iterator = treeSet.iterator();
            while(cnt < x && iterator.hasNext()) {
                p = iterator.next();
                ans[i] += p.first * p.second;
                //System.out.println(p.first + "," + p.second);
                cnt++;
            }

        }


        return ans;
    }
}
```
['Java']