java.lang.Object
g1801_1900.s1838_frequency_of_the_most_frequent_element.Solution

public class Solution extends Object
1838 - Frequency of the Most Frequent Element.<p>Medium</p> <p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong></em> <code>k</code> <em>operations</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,4], k = 5</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,4,8,13], k = 5</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> There are multiple optimal solutions:</p> <ul> <li> <p>Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.</p> </li> <li> <p>Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.</p> </li> <li> <p>Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.</p> </li> </ul> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [3,9,6], k = 2</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> <li><code>1 <= k <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxFrequency

      public int maxFrequency(int[] nums, int k)