3346. Maximum Frequency of an Element After Performing Operations I #2322
-
|
Topics: You are given an integer array You must perform an operation
Return the maximum possible frequency1 of any element in Example 1:
Example 2:
Constraints:
Hint:
Footnotes
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to find the maximum frequency of any element after performing operations where we can add values in the range ApproachThe key insight is that for any target value
The solution works by:
Let's implement this solution in PHP: 3346. Maximum Frequency of an Element After Performing Operations I <?php
/**
* @param Integer[] $nums
* @param Integer $k
* @param Integer $numOperations
* @return Integer
*/
function maxFrequency($nums, $k, $numOperations) {
$n = count($nums);
if ($n === 0) return 0;
// Find the range of possible target values
$minV = min($nums);
$maxV = max($nums);
$L = $minV - $k;
$R = $maxV + $k;
$size = $R - $L + 1;
$offset = -$L; // To map values to array indices
// Initialize arrays
$diff = array_fill(0, $size + 1, 0);
$exact = array_fill(0, $size, 0);
// For each element, mark the range of values it can be transformed to
foreach ($nums as $v) {
// The element v can be transformed to any value in [v-k, v+k]
$left = max(0, $v - $k + $offset);
$right = min($size - 1, $v + $k + $offset);
$diff[$left] += 1;
if ($right + 1 < count($diff)) {
$diff[$right + 1] -= 1;
}
// Count exact occurrences
$exactPos = $v + $offset;
if ($exactPos >= 0 && $exactPos < $size) {
$exact[$exactPos] += 1;
}
}
// Calculate prefix sums to get coverage count for each position
$ans = 0;
$current = 0;
for ($i = 0; $i < $size; $i++) {
$current += $diff[$i];
$coverage = $current; // Elements that can reach value (L + i)
$already = $exact[$i]; // Elements already equal to (L + i)
// Maximum frequency is limited by:
// 1. Number of elements that can reach this value
// 2. Number of exact matches plus available operations
$possible = min($coverage, $already + $numOperations);
if ($possible > $ans) {
$ans = $possible;
}
}
return $ans;
}
// Test cases
$nums1 = [1, 4, 5];
$k1 = 1;
$numOperations1 = 2;
echo "Output 1: " . maxFrequency($nums1, $k1, $numOperations1) . "\n"; // Expected: 2
$nums2 = [5, 11, 20, 20];
$k2 = 5;
$numOperations2 = 1;
echo "Output 2: " . maxFrequency($nums2, $k2, $numOperations2) . "\n"; // Expected: 2
// Example 3
$nums = [88, 53];
$k = 27;
$numOperations = 2;
echo "Output: " . maxFrequency($nums, $k, $numOperations) . "\n"; // Expected: 2
// Example 4
$nums = [2,70,73];
$k = 39;
$numOperations = 2;
echo "Output: " . maxFrequency($nums, $k, $numOperations) . "\n"; // Expected: 2
// Example 5
$nums = [58,80,5];
$k = 58;
$numOperations = 3;
echo "Output: " . maxFrequency($nums, $k, $numOperations) . "\n"; // Expected: 3
// Example 6
$nums = [2,49];
$k = 58;
$numOperations = 0;
echo "Output: " . maxFrequency($nums, $k, $numOperations) . "\n"; // Expected: 1
?>Explanation:
Complexity Analysis
|
Beta Was this translation helpful? Give feedback.
We need to find the maximum frequency of any element after performing operations where we can add values in the range
[-k, k]to distinct elements.Approach
The key insight is that for any target value
x, the number of elements that can be transformed toxis:x[x-k, x+k]that can be transformed toxThe solution works by:
minV-ktomaxV+k)