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

You're given an array nums of n integers and two integers k and x. Your task is to find the x-sum for every subarray of length k.

The x-sum of an array is calculated using this procedure:

  1. Count occurrences: Count how many times each element appears in the array
  2. Select top x frequent: Keep only the x most frequent elements. If two elements have the same frequency, the larger value is considered more frequent
  3. Calculate sum: Sum all occurrences of these selected elements

Special case: If the array has fewer than x distinct elements, the x-sum is simply the sum of all elements.

Goal: Return an array where answer[i] is the x-sum of the subarray nums[i..i+k-1].

Example: For array [3,8,7,8,7,5] with k=2, x=2, the subarray [8,7] has frequencies: 8β†’1, 7β†’1. Since both have same frequency, we pick both (8 > 7), so x-sum = 8 + 7 = 15.

Input & Output

example_1.py β€” Basic Case
$ Input: nums = [3,8,7,8,7,5], k = 2, x = 2
β€Ί Output: [11, 15, 15, 15, 12]
πŸ’‘ Note: For k=2, x=2: [3,8]β†’{3:1,8:1}β†’8+3=11; [8,7]β†’{8:1,7:1}β†’8+7=15; [7,8]β†’{7:1,8:1}β†’8+7=15; [8,7]β†’{8:1,7:1}β†’8+7=15; [7,5]β†’{7:1,5:1}β†’7+5=12
example_2.py β€” Repeated Elements
$ Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
β€Ί Output: [6, 10, 12]
πŸ’‘ Note: For k=6, x=2: [1,1,2,2,3,4]β†’{2:2,1:2,3:1,4:1}β†’2Γ—2+1Γ—2=6 (ties broken by value); [1,2,2,3,4,2]β†’{2:3,others:1}β†’2Γ—3+4Γ—1=10; [2,2,3,4,2,3]β†’{2:3,3:2}β†’2Γ—3+3Γ—2=12
example_3.py β€” Edge Case
$ Input: nums = [3,8,7,8,7,5], k = 2, x = 1
β€Ί Output: [8, 8, 8, 8, 7]
πŸ’‘ Note: For x=1, only pick most frequent element: [3,8]β†’8; [8,7]β†’8; [7,8]β†’8; [8,7]β†’8; [7,5]β†’7 (both have freq 1, pick larger)

Constraints

  • 1 ≀ n ≀ 5 Γ— 104
  • 1 ≀ nums[i] ≀ 109
  • 1 ≀ k ≀ n
  • 1 ≀ x ≀ k
  • n is the length of the array nums

Visualization

Tap to expand
X-Sum Calculation ProcessStep 1: Count FrequenciesWindow: [8, 7, 8]8 appears 2 times7 appears 1 timefreq = {8: 2, 7: 1}Step 2: Rank by FrequencySort by freq↓, value↓1st: (8, freq=2)2nd: (7, freq=1)ranked = [(8,2), (7,1)]Step 3: Select Top-Xx = 2, pick first 2βœ“ Keep (8, freq=2)βœ“ Keep (7, freq=1)selected = [(8,2), (7,1)]Step 4: SumCalculate x-sum8 Γ— 2 = 167 Γ— 1 = 7Total: 23Example with Tie-Breaking:Window: [3, 5, 3, 5] with x = 2Frequencies3 β†’ 2 times5 β†’ 2 timesSame frequency!Tie-Breaking5 > 3 (larger value)Order: (5,2), (3,2)Higher value winsResultTake top 2:5Γ—2 + 3Γ—2 = 16X-sum = 16🎯 Key Insight:The x-sum prioritizes frequency first, then value for ties. This ensures we always pick the most 'impactful' elements in each window.
Understanding the Visualization
1
Count Frequencies
Count how many times each element appears in the current window
2
Rank Elements
Sort elements by frequency (descending), then by value (descending) for ties
3
Select Top-X
Pick the first min(x, distinct_elements) from the ranked list
4
Calculate Sum
Multiply each selected element by its frequency and sum the results
Key Takeaway
🎯 Key Insight: Efficiently track frequencies in sliding windows and use proper tie-breaking (higher value wins) to determine the top-x elements for each subarray.
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
48.2K Views
Medium Frequency
~25 min Avg. Time
1.4K 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