#3318
Find X-Sum of All K-Long Subarrays I
newbie · 265 · lc easy +19 · 76.1% accepted · 558 likes · top 87%
Description
You are given an array nums of n integers along with two integers k and x.
The x-sum of an array is computed as follows:
- Tally how many times each distinct element appears.
- Retain only the contributions from the top x most frequent values. When two values share the same frequency, the numerically larger value is treated as more frequent.
- Sum all retained occurrences.
If fewer than x distinct values exist in the array, the x-sum equals the ordinary sum of the array.
Return an integer array answer of length n - k + 1 where answer[i] equals the x-sum of the window nums[i..i + k - 1].
Code
1
2
3