Skip to content

Practice Find X-Sum of All K-Long Subarrays II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3321
LeetCode Problem

Find X-Sum of All K-Long Subarrays II

Problem Statement

For each length-k subarray of nums, count the frequency of every distinct value. Rank distinct values by decreasing frequency, breaking ties in favor of larger values. Select the first x distinct values in that ranking, or all of them if fewer than x exist, and sum all occurrences of those selected values. This is the subarray’s x-sum; if fewer than x distinct values exist, sum the whole subarray. Return the x-sums in starting-index order.

Source: Find X-Sum of All K-Long Subarrays II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
Output
[6,10,12]

# Example 2

Input
nums = [3,8,7,8,7,5], k = 2, x = 2
Output
[11,15,15,15,12]

Constraints

nums.length == n
1 <= n <= 10^5
1 <= nums[i] <= 10^9
1 <= x <= k <= nums.length

Related LeetCode Problems

Practice more problems similar to Find X-Sum of All K-Long Subarrays II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.