↓Skip to main content
  1. LeetCode/

LeetCode 1838: Frequency of the Most Frequent Element

·2 mins· ·
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-11

  • Submissions: Accepted (first try)

Approach
#

Values can only increase, so the target is always some existing value nums[right], and the numbers raised to it should be the ones just below it (fewest increments).

After sorting, raising the window [left, right] to nums[right] costs:

cost = nums[right] × (right - left + 1) − sum(nums[left..right])

For fixed right, a larger window costs more; for fixed left, moving right right only costs more. The condition is monotonic: use the “longest” sliding window.

My Solution
#

class Solution:
    def maxFrequency(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = total = best = 0
        for right, x in enumerate(nums):
            total += x
            while x * (right - left + 1) - total > k:
                total -= nums[left]
                left += 1
            best = max(best, right - left + 1)
        return best

My first accepted version tracked a “current target” and a running cost, topping up the difference when the target grew, which was convoluted. Computing “target × length − sum” directly is clearer.

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • The target is not arbitrary: it is the window’s rightmost (largest) value.
  • Costs get large; fine in Python, but other languages need 64-bit integers.

Interview Explanation
#

Only increments are allowed, so the target is an existing value, raised to from the closest smaller values. I sort and slide a window with the right end as the target; the cost is the right end times the length minus the sum, and I shrink the left when it exceeds k, tracking the longest length. Time O(n log n).

Related

LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not