2Brute2Best
🔥0

Frequency of the Most Frequent Element

Medium·LeetCode #1838 Pro OnlyTricky / Edge Case
LeetCode
01

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.

Input

An integer array nums and an integer k.

Output

The maximum possible count of one value after at most k increments.

Example 1
Input: nums = [1,2,4], k = 5
Output: 3

Raise 1 three times and 2 twice: 3 + 2 = 5 operations. Now the array is [4,4,4], so 4 appears 3 times.

Example 2
Input: nums = [1,4,8,13], k = 5
Output: 2

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.

Example 3
Input: nums = [3,9,6], k = 2
Output: 1

Every pair is at least 3 apart, so 2 operations can't make any two values equal.

Constraints
  • • 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.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“increment an element by 1, at most k times”
→ A budget k — the window's cost must stay ≤ k.
“maximum frequency”
→ Biggest group of equal values → longest valid window.
“any element (order doesn't matter)”
→ You may sort first. Sorting turns 'a group of close values' into a contiguous block.
03

Approach Complexity Ladder

Brute Force — sort, then try every targetO(N²)O(1)

For each target nums[r], raise the nearest smaller values until k runs out

Better — prefix sums + binary searchO(N log N)O(N)

Cost of a block is one formula; binary search the furthest left start

Optimal — sort + variable sliding windowO(N log N)O(1)

Sort; window is valid when nums[right] × size − sum ≤ k; track the largest window

Brute Force Intuition

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:

Window grows and shrinks. Expand right; when the window breaks the rule, move left until it is valid again. Works when "bigger window = harder to be valid".
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.