Frequency of the Most Frequent Element

The **frequency** of an element is the number of times it occurs in an array. You are given an integer array `nums` and an integer `k`. In one operation you can choose an index and increase `nums[index]` by `1`. Return the **maximum possible frequency** of an element after performing **at most** `k` operations.

30:00

Frequency of the Most Frequent Element
medium
Free written solution
Topics
Companies

The frequency of an element is the number of times it occurs in an array.

You are given an integer array nums and an integer k. In one operation you can choose an index and increase nums[index] by 1.

Return the maximum possible frequency of an element after performing at most k operations.

Example 1:
Input: {"nums":[1,2,4],"k":5}
Output: 3
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^5

  • 1≤k≤1051 \leq k \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,4],"k":5}

Only increases are allowed, so a group can only be raised up to its largest member. The budget is k = 5 single +1 steps.

nums (unsorted)

1

0

2

1

4

2

cost

0 / 5

window size: 0

best: 0

Variables
VariableValue
k5
left0
right-1
total0
best0
DepthFunction Call
Stack empty
0/8