在 Array 元素中进行 K 次替换后最大化元素的出现次数
给定一个具有N个整数的数组arr[]和一个整数K ,任务是找到一个数组,使得它在数组元素内进行K次替换后最多包含单个元素。
例子:
Input: N = 7, arr[] = {1, 2, 1, 5, 1, 6, 7}, K = 3
Output: {1, 1, 1, 1, 1, 1, 7}
Explanation: We can replace element on index 1, 3, and 5 with 1, i.e. A[1] = A[0], A[3] = A[0] and A[5] = A[0]
Input: N = 6, arr[] = {2, 2, 2, 5, 1, 6 }, K = 3
Output: {2, 2, 2, 2, 2, 2 }
方法:解决这个问题的方法是基于以下思想:
- If we dont do any replacement, and if there are duplicates present in the array, then there will be an element present already occurring maximum number of times in the array.
- Now when K replacements are allowed, we can simply try to change replace elements not equal to maximum occurring element with maximum occurring element K times, to get the desired array.
以下是实现上述方法的算法:
- 创建一个哈希图来存储每个不同元素的频率。
- 查找数组中出现的最大元素。
- 然后分配最大出现元素的每个元素值。
- 确保在分配元素之前和分配元素之后注意频率。
- 为此,首先降低当前元素的频率,并在为其分配最大出现值后,增加其在哈希图中的值。
下面是上述方法的实现:
C++
// C++ code for the above discussed approach
#include
using namespace std;
// Function to maximizeTheElements
vector maximizeTheElements(
int N, vector arr, int K)
{
// Map tp store the frequency
// of the elements
unordered_map mp;
int max_freq = 0, max_element = -1;
for (auto x : arr) {
mp[x]++;
// Getting max_element
if (mp[x] > max_freq) {
max_freq = mp[x];
max_element = x;
}
}
for (int i = 0; i < N && K > 0; i++) {
// If the element is not equal
// to the max_element
if (arr[i] != max_element) {
// Decrease its frequency from the map
mp[arr[i]] -= 1;
// Assign the max frequency element
arr[i] = max_element;
// Increase its frequency in the map
mp[arr[i]] += 1;
// Decrease the operation by 1
K -= 1;
}
}
// Return the modified array
return arr;
}
// Driver Function
int main()
{
int N = 7;
vector arr = { 1, 2, 1, 5, 1, 6, 7 };
int K = 3;
// Function call
vector res = maximizeTheElements(N, arr, K);
for (auto x : res) {
cout << x << " ";
}
cout << endl;
return 0;
}
Java
// Java code for the above discussed approach
import java.util.*;
class GFG {
// Function to maximizeTheElements
static int[] maximizeTheElements(
int N, int arr[], int K)
{
// Map tp store the frequency
// of the elements
HashMap m = new HashMap();
int max_freq = 0, max_element = -1;
for(int i = 0; i < arr.length; i++){
if(m.containsKey(arr[i])){
m.put(arr[i], m.get(arr[i]) + 1);
}
else{
m.put(arr[i],1);
}
// Getting max_element
if (m.get(arr[i]) > max_freq) {
max_freq = m.get(arr[i]);
max_element = arr[i];
}
}
for (int i = 0; i < N; i++) {
if(K <= 0)break;
// If the element is not equal
// to the max_element
if (arr[i] != max_element) {
// Decrease its frequency from the map
m.put(arr[i], m.get(arr[i]) - 1);
// Assign the max frequency element
arr[i] = max_element;
// Increase its frequency in the map
m.put(arr[i], m.get(arr[i]) + 1);
// Decrease the operation by 1
K -= 1;
}
}
// Return the modified array
return arr;
}
// Driver Function
public static void main (String[] args) {
int N = 7;
int arr[] = { 1, 2, 1, 5, 1, 6, 7 };
int K = 3;
// Function call
int res[] = maximizeTheElements(N, arr, K);
for (int x : res) {
System.out.print(x + " ");
}
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 code for the above discussed approach
# Function to maximizeTheElements
def maximizeTheElements(N, arr, K):
# Map tp store the frequency
# of the elements
mp = {}
max_freq, max_element = 0, -1
for x in arr:
mp[x] = mp[x] + 1 if x in mp else 1
# Getting max_element
if (mp[x] > max_freq):
max_freq = mp[x]
max_element = x
for i in range(0, N):
if K <= 0:
break
# If the element is not equal
# to the max_element
if (arr[i] != max_element):
# Decrease its frequency from the map
mp[arr[i]] -= 1
# Assign the max frequency element
arr[i] = max_element
# Increase its frequency in the map
mp[arr[i]] += 1
# Decrease the operation by 1
K -= 1
# Return the modified array
return arr
# Driver Function
if __name__ == "__main__":
N = 7
arr = [1, 2, 1, 5, 1, 6, 7]
K = 3
# Function call
res = maximizeTheElements(N, arr, K)
for x in res:
print(x, end=" ")
# This code is contributed by rakeshsahni
Javascript
输出
1 1 1 1 1 1 7
时间复杂度: O(N)
辅助空间: O(N)