Skip to content

Commit

Permalink
add 1838. Frequency of the Most Frequent Element
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeed205 committed Nov 18, 2023
1 parent f50ddb3 commit f667794
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Arrays;

public class Solution {
public int maxFrequency(int[] nums, int k) {
Arrays.sort(nums); // Sort the array

int maxFreq = 0;
int left = 0;
long sum = 0; // Use long to avoid integer overflow

for (int right = 0; right < nums.length; right++) {
sum += nums[right];

// Check if we need to shrink the window
while ((right - left + 1) * (long) nums[right] - sum > k) {
sum -= nums[left];
left += 1;
}

// Update the maximum frequency
maxFreq = Math.max(maxFreq, right - left + 1);
}

return maxFreq;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxFrequency = function (nums, k) {
nums.sort((a, b) => a - b); // Sort the array

let maxFreq = 0;
let left = 0;
let sum = 0;

for (let right = 0; right < nums.length; right++) {
sum += nums[right];

// Check if we need to shrink the window
while ((right - left + 1) * nums[right] - sum > k) {
sum -= nums[left];
left += 1;
}

// Update the maximum frequency
maxFreq = Math.max(maxFreq, right - left + 1);
}

return maxFreq;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List

class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort() # Sort the array

max_freq = 0
left = 0
sum_val = 0

for right in range(len(nums)):
sum_val += nums[right]

# Check if we need to shrink the window
while (right - left + 1) * nums[right] - sum_val > k:
sum_val -= nums[left]
left += 1

# Update the maximum frequency
max_freq = max(max_freq, right - left + 1)

return max_freq
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
impl Solution {
pub fn max_frequency(nums: Vec<i32>, k: i32) -> i32 {
let mut nums = nums;
nums.sort(); // Sort the array

let mut max_freq = 0;
let mut left = 0;
let mut sum = 0;

for right in 0..nums.len() {
sum += nums[right];

// Check if we need to shrink the window
while (right - left + 1) as i32 * nums[right] - sum > k {
sum -= nums[left];
left += 1;
}

// Update the maximum frequency
max_freq = max_freq.max(right - left + 1);
}

max_freq as i32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function maxFrequency(nums: number[], k: number): number {
nums.sort((a, b) => a - b); // Sort the array

let maxFreq = 0;
let left = 0;
let sum = 0;

for (let right = 0; right < nums.length; right++) {
sum += nums[right];

// Check if we need to shrink the window
while ((right - left + 1) * nums[right] - sum > k) {
sum -= nums[left];
left += 1;
}

// Update the maximum frequency
maxFreq = Math.max(maxFreq, right - left + 1);
}

return maxFreq;
}
40 changes: 40 additions & 0 deletions Medium/1838. Frequency of the Most Frequent Element/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## [1838. Frequency of the Most Frequent Element](https://leetcode.com/problems/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:

<pre>
<strong>Input:</strong> nums = [1,2,4], k = 5
<strong>Output:</strong> 3
<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.
</pre>

#### Example 2:

<pre>
<strong>Input:</strong> nums = [1,4,8,13], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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.
</pre>

#### Example 3:

<pre>
<strong>Input:</strong> nums = [3,9,6], k = 2
<strong>Output:</strong> 1
</pre>

#### Constraints:

- <code>1 <= nums.length <= 10<sup>5</sup></code>
- <code>1 <= nums[i] <= 10<sup>5</sup></code>
- <code>1 <= k <= 10<sup>5</sup></code>

0 comments on commit f667794

Please sign in to comment.