3318. Find X-Sum of All K-Long Subarrays I
You are given an array
nums
ofn
integers and two integersk
andx
.The x-sum of an array is calculated by the following procedure:
- Count the occurrences of all elements in the array.
- Keep only the occurrences of the top
x
most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.- Calculate the sum of the resulting array.
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 lengthn - k + 1
whereanswer[i]
is the x-sum of the subarraynums[i..i + k - 1]
.A subarray is a contiguous non-empty sequence of elements within an array.
c++
1 | struct Node { |