Skip to content

visual walkthrough

Frequency of the Most Frequent Element

Sorting plus a window-sum budget check.

Solve on LeetCode

The idea

To make several numbers equal using only increments, raise them all to the largest one among them. After sorting, the cheapest numbers to raise are the ones just below the target, so we only look at windows of neighbours.

The cost of a window is (largest × length) − (sum). Keep the window as long as that stays within k.

Complexity

approachtimespace
Each target, reach backO(n²)O(1)
Sort + sliding windowO(n log n)O(1)

More walkthroughs