Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 0-indexed integer array nums and an integer k.
You can perform the following operation on the array at most k times:
i from the array and increase or decrease nums[i] by 1.The score of the final array is the frequency of the most frequent element in the array.
Return the maximum score you can achieve.
The frequency of an element is the number of occurences of that element in the array.
Example 1:
Input: nums = [1,2,6,4], k = 3 Output: 3 Explanation: We can do the following operations on the array: - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2]. The element 2 is the most frequent in the final array so our score is 3. It can be shown that we cannot achieve a better score.
Example 2:
Input: nums = [1,4,4,2,4], k = 0 Output: 3 Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1090 <= k <= 1014Problem summary: You are given a 0-indexed integer array nums and an integer k. You can perform the following operation on the array at most k times: Choose any index i from the array and increase or decrease nums[i] by 1. The score of the final array is the frequency of the most frequent element in the array. Return the maximum score you can achieve. The frequency of an element is the number of occurences of that element in the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Sliding Window
[1,2,6,4] 3
[1,4,4,2,4] 0
frequency-of-the-most-frequent-element)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
class Solution {
public int maxFrequencyScore(int[] nums, long k) {
Arrays.sort(nums);
int n = nums.length;
long[] s = new long[n + 1];
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + nums[i - 1];
}
int l = 0, r = n;
while (l < r) {
int mid = (l + r + 1) >> 1;
boolean ok = false;
for (int i = 0; i <= n - mid; i++) {
int j = i + mid;
int x = nums[(i + j) / 2];
long left = ((i + j) / 2 - i) * (long) x - (s[(i + j) / 2] - s[i]);
long right = (s[j] - s[(i + j) / 2]) - ((j - (i + j) / 2) * (long) x);
if (left + right <= k) {
ok = true;
break;
}
}
if (ok) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
// Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
func maxFrequencyScore(nums []int, k int64) int {
sort.Ints(nums)
n := len(nums)
s := make([]int64, n+1)
for i := 1; i <= n; i++ {
s[i] = s[i-1] + int64(nums[i-1])
}
l, r := 0, n
for l < r {
mid := (l + r + 1) >> 1
ok := false
for i := 0; i <= n-mid; i++ {
j := i + mid
x := int64(nums[(i+j)/2])
left := (int64((i+j)/2-i) * x) - (s[(i+j)/2] - s[i])
right := (s[j] - s[(i+j)/2]) - (int64(j-(i+j)/2) * x)
if left+right <= k {
ok = true
break
}
}
if ok {
l = mid
} else {
r = mid - 1
}
}
return l
}
# Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
class Solution:
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
nums.sort()
s = list(accumulate(nums, initial=0))
n = len(nums)
l, r = 0, n
while l < r:
mid = (l + r + 1) >> 1
ok = False
for i in range(n - mid + 1):
j = i + mid
x = nums[(i + j) // 2]
left = ((i + j) // 2 - i) * x - (s[(i + j) // 2] - s[i])
right = (s[j] - s[(i + j) // 2]) - ((j - (i + j) // 2) * x)
if left + right <= k:
ok = True
break
if ok:
l = mid
else:
r = mid - 1
return l
// Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
// class Solution {
// public int maxFrequencyScore(int[] nums, long k) {
// Arrays.sort(nums);
// int n = nums.length;
// long[] s = new long[n + 1];
// for (int i = 1; i <= n; i++) {
// s[i] = s[i - 1] + nums[i - 1];
// }
// int l = 0, r = n;
// while (l < r) {
// int mid = (l + r + 1) >> 1;
// boolean ok = false;
//
// for (int i = 0; i <= n - mid; i++) {
// int j = i + mid;
// int x = nums[(i + j) / 2];
// long left = ((i + j) / 2 - i) * (long) x - (s[(i + j) / 2] - s[i]);
// long right = (s[j] - s[(i + j) / 2]) - ((j - (i + j) / 2) * (long) x);
// if (left + right <= k) {
// ok = true;
// break;
// }
// }
//
// if (ok) {
// l = mid;
// } else {
// r = mid - 1;
// }
// }
//
// return l;
// }
// }
// Accepted solution for LeetCode #2968: Apply Operations to Maximize Frequency Score
function maxFrequencyScore(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
s[i] = s[i - 1] + nums[i - 1];
}
let l: number = 0;
let r: number = n;
while (l < r) {
const mid: number = (l + r + 1) >> 1;
let ok: boolean = false;
for (let i = 0; i <= n - mid; i++) {
const j = i + mid;
const x = nums[Math.floor((i + j) / 2)];
const left = (Math.floor((i + j) / 2) - i) * x - (s[Math.floor((i + j) / 2)] - s[i]);
const right = s[j] - s[Math.floor((i + j) / 2)] - (j - Math.floor((i + j) / 2)) * x;
if (left + right <= k) {
ok = true;
break;
}
}
if (ok) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.