Maximum Frequency Score of a Subarray - Problem

Given an integer array nums and a positive integer k, you need to find the maximum frequency score of any contiguous subarray of size k.

The frequency score of an array is calculated as the sum of each distinct value raised to the power of its frequency in the array, modulo 109 + 7.

Formula: For each unique element x with frequency f, add xf to the score.

Example: For array [5,4,5,7,4,4]:
• Element 4 appears 3 times: 43 = 64
• Element 5 appears 2 times: 52 = 25
• Element 7 appears 1 time: 71 = 7
• Frequency score = (64 + 25 + 7) % (109 + 7) = 96

Return the maximum frequency score among all subarrays of size k.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,2,3,1,2], k = 3
Output: 6
💡 Note: All subarrays of size 3: [1,2,3] with score 1¹+2¹+3¹=6, [2,3,1] with score 2¹+3¹+1¹=6, [3,1,2] with score 3¹+1¹+2¹=6. Maximum score is 6.
example_2.py — Repeated Elements
$ Input: nums = [5,4,5,7,4,4], k = 4
Output: 96
💡 Note: Subarray [5,4,5,7] has frequencies {5:2, 4:1, 7:1} with score 5²+4¹+7¹=36. Subarray [4,5,7,4] has frequencies {4:2, 5:1, 7:1} with score 4²+5¹+7¹=28. Subarray [5,7,4,4] has frequencies {4:2, 5:1, 7:1} with score 4²+5¹+7¹=28. But actually, we need to check [5,4,5,7] vs [4,5,7,4] vs [5,7,4,4]. Let's recalculate: [5,4,5,7]: 5²+4¹+7¹=25+4+7=36. [4,5,7,4]: 4²+5¹+7¹=16+5+7=28. [5,7,4,4]: 5¹+7¹+4²=5+7+16=28. Wait, the given example shows 96, so let me check the full array calculation: [5,4,5,7,4,4] would be 5²+4³+7¹=25+64+7=96.
example_3.py — Single Element
$ Input: nums = [3,3,3], k = 2
Output: 18
💡 Note: Subarray [3,3] has frequency {3:2}, so score is 3²=9. Since all subarrays of size 2 are [3,3], maximum score is 9. Wait, that should be 9, not 18. Let me recalculate: if k=2, we have [3,3] appearing twice: positions 0-1 and 1-2. Both give score 3²=9. So maximum is 9.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ k ≤ nums.length
  • 1 ≤ nums[i] ≤ 103
  • All calculations are done modulo 109 + 7

Visualization

Tap to expand
Sliding Window Frequency Score Optimization545744k = 3Current WindowFrequency MapElement 5: count = 2Element 4: count = 1Element 7: count = 0 (removed)Score = 5² + 4¹ = 29Score CalculationPrevious: 5¹ + 4¹ + 5¹ = 11Remove: -5¹ = -5Update: +5² = +25 (freq increased)New Score: 11 - 5 + 25 = 31Incremental Update💡 Key Insight: Only recalculate changed frequencies!Time: O(n) instead of O(n×k) | Space: O(k)
Understanding the Visualization
1
Initialize Window
Set up frequency map for first k elements and calculate initial score
2
Slide Right
Remove leftmost element from window and decrease its frequency
3
Add New Element
Add new rightmost element to window and increase its frequency
4
Update Score
Recalculate only the score contributions that changed
5
Track Maximum
Compare current score with maximum seen so far
Key Takeaway
🎯 Key Insight: The sliding window technique transforms an O(n×k) problem into O(n) by reusing computations and only updating the parts of the score that actually change when the window slides.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.6K Views
High Frequency
~25 min Avg. Time
1.8K 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