Frequency of the Most Frequent Element
Problem Statement
You are given an array nums and a number k.
In one operation you pick any element and increase it by 1. You can do at most k operations in total (spread over any elements you like).
The frequency of a value is how many times it appears in the array. Return the largest frequency any value can have after at most k operations.
An integer array nums and an integer k.
The maximum possible count of one value after at most k increments.
Raise 1 three times and 2 twice: 3 + 2 = 5 operations. Now the array is [4,4,4], so 4 appears 3 times.
Several ways give 2: raise 1 to 4 (3 ops), or 4 to 8 (4 ops), or 8 to 13 (5 ops). You can't get three equal values with only 5 operations.
Every pair is at least 3 apart, so 2 operations can't make any two values equal.
- • 1 ≤ nums.length ≤ 10^5
- • 1 ≤ nums[i] ≤ 10^5
- • 1 ≤ k ≤ 10^5
- • nums[i] × length can reach 10^10 — use a 64-bit type.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each target nums[r], raise the nearest smaller values until k runs out
Cost of a block is one formula; binary search the furthest left start
Sort; window is valid when nums[right] × size − sum ≤ k; track the largest window
Sort the array. For every index r, pretend everything will become nums[r]. Walk left from r, raising each element to nums[r] and adding up the cost. Stop when the cost goes over k. The number of elements you managed to raise is the frequency for this target.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.