1838. Frequency of the Most Frequent Element LeetCode Solution

In this guide, you will get 1838. Frequency of the Most Frequent Element LeetCode Solution with the best time and space complexity. The solution to Frequency of the Most Frequent Element problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Frequency of the Most Frequent Element solution in C++
  4. Frequency of the Most Frequent Element solution in Java
  5. Frequency of the Most Frequent Element solution in Python
  6. Additional Resources
1838. Frequency of the Most Frequent Element LeetCode Solution image

Problem Statement of 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 of nums and increment the element at that 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
Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
4 has a frequency of 3.
Example 2:

Input: nums = [1,4,8,13], k = 5
Output: 2
Explanation: There are multiple optimal solutions:
– Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
– Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
– Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.

Example 3:

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

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 105
1 <= k <= 105

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

1838. Frequency of the Most Frequent Element LeetCode Solution in C++

class Solution {
 public:
  int maxFrequency(vector<int>& nums, int k) {
    int ans = 0;
    long sum = 0;

    ranges::sort(nums);

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      sum += nums[r];
      while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))
        sum -= nums[l++];
      ans = max(ans, r - l + 1);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1838. Frequency of the Most Frequent Element LeetCode Solution in Java

class Solution {
  public int maxFrequency(int[] nums, int k) {
    int ans = 0;
    long sum = 0;

    Arrays.sort(nums);

    for (int l = 0, r = 0; r < nums.length; ++r) {
      sum += nums[r];
      while (sum + k < (long) nums[r] * (r - l + 1))
        sum -= nums[l++];
      ans = Math.max(ans, r - l + 1);
    }

    return ans;
  }
}
// code provided by PROGIEZ

1838. Frequency of the Most Frequent Element LeetCode Solution in Python

class Solution:
  def maxFrequency(self, nums: list[int], k: int) -> int:
    ans = 0
    summ = 0

    nums.sort()

    l = 0
    for r, num in enumerate(nums):
      summ += num
      while summ + k < num * (r - l + 1):
        summ -= nums[l]
        l += 1
      ans = max(ans, r - l + 1)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.