Find X-Sum of All K-Long Subarrays I | Sliding Window + Mapping + No Sorting | Detailed Explanation
Approach
Map the frequency of each elements.
If it has duplicate, get the maximum frequency. Get the highest index with frequency if not.
Add the product of (fr
# Approach
1. Map the frequency of each elements.
2. If it has duplicate, get the maximum frequency. Get the highest index with frequency if not.
3. Add the product of (frequency * index + 1) to the answers array.

# Complexity
- Time complexity (***99.08%***):
$$O(N∗K)$$ 

- Space complexity (***90.79%***):
$$O(1)$$ 

# Code
```java []
class Solution {
    public int[] findXSum(int[] nums, int k, int x) {
        int n = nums.length;
        int[] freqMap = new int[50];
        int[] ans = new int[n - k + 1];
        int tempMax = 0;
        int max = 0;
        boolean dup = false;

        for (int i = 0; i < n - k + 1; i++)
        {
            freqMap = new int[50]; // Frequency Map
            max = 0;

            // Map each frequency of elements and store the maximum element
            for (int j = i; j < i + k; j++)
            {
                freqMap[nums[j] - 1]++; 
                if (nums[j] > max) max = nums[j];
            }

            // Loop 'x' times
            for (int z = x; z > 0; z--) 
            {
                tempMax = max - 1;
                dup = false;

                // Check for duplicates (Greater than 1 frequency)
                for (int f : freqMap) 
                {
                    if (f > 1)
                    {
                        dup = true;
                        break;
                    }
                }

                // Iterate through frequency map starting from the end for easier access to highest indeces
                for (int j = freqMap.length - 1; j >= 0; j--) 
                {
                    if (freqMap[j] == 0) continue; // Skip all 0 frequency

                    if (dup)
                    {
                        // If flagged duplicate, get the highest frequency index
                        if (freqMap[j] > freqMap[tempMax]) tempMax = j;
                    }
                    else 
                    {
                        // If not flagged duplicate, get the highest index with frequency
                        tempMax = j;
                        break;
                    }
                }
                
                // Add the total to the answers array
                // (element * index + 1)
                ans[i] += (freqMap[tempMax] * (tempMax + 1));
                freqMap[tempMax] = 0; // Remove used frequency
            }
        }

        return ans;
    }
}
```

![upvote.jpg](https://assets.leetcode.com/users/images/36182cee-a274-468a-b01e-e73ac3476123_1735235719.0732708.jpeg)

['Array', 'Hash Table', 'Sliding Window', 'Java']