Find X-Sum of All K-Long Subarrays I - Problem
Find X-Sum of All K-Long Subarrays I

You're given an array nums of n integers and two important parameters: k (subarray length) and x (number of top frequent elements to consider).

Your task is to calculate the x-sum for every possible subarray of length k. The x-sum is a special calculation that focuses on the most frequent elements:

How to calculate x-sum:
1. Count frequency of all elements in the subarray
2. Select the top x most frequent elements
3. If frequencies are tied, prioritize the larger value
4. Sum all occurrences of these selected elements
5. If subarray has fewer than x distinct elements, just sum the entire subarray

Example: For subarray [3, 2, 2, 3, 3] with x=2:
• Element 3 appears 3 times, element 2 appears 2 times
• Top 2 most frequent: 3 (freq=3) and 2 (freq=2)
• X-sum = 3×3 + 2×2 = 9 + 4 = 13

Return an array where each element represents the x-sum of the corresponding k-length subarray.

Input & Output

example_1.py — Basic X-Sum Calculation
$ Input: nums = [1, 1, 2, 2, 3, 4, 2, 3], k = 6, x = 2
Output: [6, 10, 12]
💡 Note: For subarray [1,1,2,2,3,4] with x=2: frequencies are {1:2, 2:2, 3:1, 4:1}. Top 2 most frequent (tie-break by larger value): 2 appears 2 times, 1 appears 2 times. X-sum = 2×2 + 1×2 = 6.
example_2.py — Tie-breaking by Value
$ Input: nums = [3, 8, 7, 8, 7, 5], k = 2, x = 2
Output: [11, 15, 15, 15, 12]
💡 Note: For subarray [3,8]: both appear once, so we take both. X-sum = 3×1 + 8×1 = 11. For [8,7]: X-sum = 8×1 + 7×1 = 15.
example_3.py — Fewer than X Distinct Elements
$ Input: nums = [1, 1, 1], k = 3, x = 5
Output: [3]
💡 Note: Subarray [1,1,1] has only 1 distinct element, which is less than x=5. So x-sum equals the total sum: 1+1+1 = 3.

Constraints

  • 1 ≤ n ≤ 50
  • 1 ≤ k ≤ n
  • 1 ≤ x ≤ k
  • 1 ≤ nums[i] ≤ 50
  • All elements fit in 32-bit integers

Visualization

Tap to expand
X-Sum Calculation ProcessInput Subarray: [3, 2, 2, 3, 3, 1] with x=2322331Step 1: Count FrequenciesFrequency MapElement 3: appears 3 timesElement 2: appears 2 timesElement 1: appears 1 timeStep 2: Sort by PriorityPriority Order1st: Element 3 (freq=3)2nd: Element 2 (freq=2)3rd: Element 1 (freq=1)Step 3: Select Top X=2Selected Elements✓ Element 3 (freq=3)✓ Element 2 (freq=2)✗ Element 1 (freq=1)Step 4: Calculate X-SumFinal CalculationElement 3 contributes: 3 × 3 = 9Element 2 contributes: 2 × 2 = 4X-Sum = 9 + 4 = 13🎯 Key Insight: X-sum focuses on most frequent elements, using value as tiebreaker to ensure deterministic results
Understanding the Visualization
1
Count Frequencies
Build a frequency map for all elements in the current subarray
2
Sort by Priority
Sort elements first by frequency (descending), then by value (descending) for ties
3
Select Top X
Take the top x elements according to the priority order
4
Calculate Sum
Sum all occurrences of the selected top x elements
Key Takeaway
🎯 Key Insight: X-sum prioritizes frequent elements while using value-based tie-breaking, making it perfect for scenarios where you need to focus on the most significant contributors in a dataset.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
32.0K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen