Watch 10 video solutions for Find X-Sum of All K-Long Subarrays I, a easy level problem involving Array, Hash Table, Sliding Window. This walkthrough by codestorywithMIK has 7,476 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array nums of n integers and two integers k and x.
The x-sum of an array is calculated by the following procedure:
x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.Note that if an array has less than x distinct elements, its x-sum is the sum of the array.
Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].
Example 1:
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
Output: [6,10,12]
Explanation:
[1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.[1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.[2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.Example 2:
Input: nums = [3,8,7,8,7,5], k = 2, x = 2
Output: [11,15,15,15,12]
Explanation:
Since k == x, answer[i] is equal to the sum of the subarray nums[i..i + k - 1].
Constraints:
1 <= n == nums.length <= 501 <= nums[i] <= 501 <= x <= k <= nums.lengthProblem Overview: You receive an array nums and integers k and x. For every contiguous subarray of length k, compute the x-sum. The x-sum is formed by selecting the x numbers with the highest frequency in that window (tie broken by larger value) and summing frequency * value for those numbers. The result is an array containing the x-sum for each window.
Approach 1: Brute Force with Sorting (Time: O(n * k log k), Space: O(k))
Process each window independently. For every starting index, iterate through the next k elements and build a frequency map using a hash table. Convert the map entries into a list of (value, frequency) pairs. Sort this list by frequency descending, and if frequencies match, by value descending. Take the first x elements and add frequency * value to compute the window’s x-sum.
This approach is straightforward and mirrors the definition of the problem. The drawback is repeated work: each window rebuilds the frequency map and performs sorting again. Sorting up to k unique elements costs O(k log k), and this happens for roughly n windows. Still, this solution is easy to implement and works well when k is small.
Approach 2: Optimized Sliding Window with Heap (Time: O(n log k), Space: O(k))
Instead of recomputing everything for each window, maintain a running frequency map while sliding the window across the array. Add the new element entering the window and decrement the frequency of the element leaving it. This technique uses the classic sliding window pattern to avoid rebuilding counts.
To efficiently determine the top x elements by frequency and value, maintain a priority queue (max heap) containing pairs representing the current frequency and value. Each time the window changes, push updated entries into the heap. When computing the x-sum, repeatedly extract the best candidates from the heap while discarding outdated entries whose stored frequency no longer matches the value in the frequency map.
This method reduces the need to sort the entire frequency list every time. Heap operations cost O(log k), and each element update happens only when the window moves. The combination of a hash table for counting and a heap (priority queue) for ranking elements makes the process efficient for larger inputs.
Recommended for interviews: Start by explaining the brute force approach to demonstrate understanding of the x-sum definition and ordering rule. Then transition to the optimized sliding window solution. Interviewers expect candidates to recognize that overlapping windows share most of their elements, so maintaining frequencies incrementally and using a heap to retrieve the top x contributors shows strong algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with Sorting | O(n * k log k) | O(k) | Simple implementation or when k is small and performance constraints are loose |
| Sliding Window with Heap | O(n log k) | O(k) | Large arrays where recomputing frequencies and sorting every window is too slow |