通过最多增加或减少所有数组元素来最大化元素的频率 |设置 2
给定一个大小为N的数组arr[] ,任务是通过最多将每个数组元素递增或递减1来找到任何数组元素的最大频率。
例子:
Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 }
Output: 4
Explanation:
Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1, 4, 1, 5, 9, 2 }
Incrementing the value of arr[1] by 1 modifies arr[] to { 2, 2, 4, 1, 5, 9, 2 }
Incrementing the value of arr[3] by 1 modifies arr[] to { 2, 2, 4, 1, 5, 9, 2 }
Therefore, the frequency of an array element(arr[0]) is 4 which is the maximum possible.
Input: arr[] = { 0, 1, 2, 3, 4, 5, 6 }
Output: 3
Explanation:
Incrementing the value of arr[0] by 1 modifies arr[] to { 1, 1, 2, 3, 4, 5, 6 }
Decrementing the value of arr[2] by 1 modifies arr[] to { 1, 1, 1, 3, 4, 5, 6 }
Therefore, the frequency of an array element(arr[0]) is 3 which is the maximum possible.
贪心方法:解决此问题的贪心方法已在本文的第 1 组中进行了讨论。
频率计数方法:这个想法是创建一个频率数组并存储数组arr[i] 的所有元素的频率。现在,对于 element 的每个可能值,尝试将左右值合并到这一点,即freq[i] + freq[i-1] + freq[i+1]。请按照以下步骤解决问题:
- 定义一个值为1e5的变量MAXN 。
- 用值0初始化数组freq[MAXN] 。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 将freq[arr[i]]的值增加1。
- 用值 -MAXN 初始化变量max_freq 。
- 使用变量i遍历范围[1, MAXN-1)并执行以下任务:
- 将max_freq的值设置为max_freq或freq[i-1] + freq[i] + freq[i+1] 的最大值。
- 执行上述步骤后,打印max_freq的值作为答案。
下面是上述方法的实现。
C++
// C++ program to implement the above approach
#include
using namespace std;
#define MAXN 100005
// Function to maximize the frequency
// of an array element by incrementing or
// decrementing array elements at most once
void max_freq(int arr[], int N)
{
// Store the frequency of each element
int freq[MAXN];
memset(freq, 0, sizeof(freq));
for (int i = 0; i < N; i++) {
freq[arr[i]]++;
}
// Iterate through each value
// try to merge left and
// right values to it
int max_freq = -MAXN;
for (int i = 1; i < MAXN - 1; i++) {
max_freq = max(max_freq,
freq[i] + freq[i - 1]
+ freq[i + 1]);
}
cout << max_freq;
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 4, 1, 5, 9, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
max_freq(arr, N);
return 0;
}
Java
// Java program to implement the above approach
import java.util.Arrays;
class GFG
{
public static int MAXN = 100005;
// Function to maximize the frequency
// of an array element by incrementing or
// decrementing array elements at most once
public static void max_freq(int arr[], int N)
{
// Store the frequency of each element
int[] freq = new int[MAXN];
Arrays.fill(freq, 0);
for (int i = 0; i < N; i++) {
freq[arr[i]]++;
}
// Iterate through each value
// try to merge left and
// right values to it
int max_freq = -MAXN;
for (int i = 1; i < MAXN - 1; i++) {
max_freq = Math.max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
}
System.out.println(max_freq);
}
// Driver Code
public static void main(String args[]) {
int arr[] = { 3, 1, 4, 1, 5, 9, 2 };
int N = arr.length;
// Function call
max_freq(arr, N);
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python3 program to implement the above approach
MAXN = 100005;
# Function to maximize the frequency
# of an array element by incrementing or
# decrementing array elements at most once
def max_freq(arr, N) :
# Store the frequency of each element
freq = [0] * MAXN;
for i in range(N) :
freq[arr[i]] += 1;
# Iterate through each value
# try to merge left and
# right values to it
max_freq = -MAXN;
for i in range(1, MAXN - 1) :
max_freq = max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
print(max_freq);
# Driver Code
if __name__ == "__main__" :
arr = [ 3, 1, 4, 1, 5, 9, 2 ];
N = len(arr);
# Function call
max_freq(arr, N);
# This code is contributed by AnkThon
C#
// C# program to implement the above approach
using System;
class GFG
{
static int MAXN = 100005;
// Function to maximize the frequency
// of an array element by incrementing or
// decrementing array elements at most once
static void max_freq(int []arr, int N)
{
// Store the frequency of each element
int []freq = new int[MAXN];
Array.Clear(freq, 0, freq.Length);
for (int i = 0; i < N; i++) {
freq[arr[i]]++;
}
// Iterate through each value
// try to merge left and
// right values to it
int max_freq = -MAXN;
for (int i = 1; i < MAXN - 1; i++) {
max_freq = Math.Max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
}
Console.Write(max_freq);
}
// Driver Code
public static void Main() {
int []arr = { 3, 1, 4, 1, 5, 9, 2 };
int N = arr.Length;
// Function call
max_freq(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
4
时间复杂度: O(N)
辅助空间: O(|Max|),其中 Max 是数组中的最大元素