给定一个大小为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.
方法:该问题可以使用贪心技术解决。这个想法是找到数组中存在的最大和最小元素,并通过迭代数组元素范围内的所有数字来计算三个连续数字的频率之和。最后,打印获得的最大和。请按照以下步骤解决问题:
- 找到数组的最大元素,比如Max 。
- 找到数组的最小元素,比如Min 。
- 计算数组中[Min, Max]范围内所有数组元素的频率。
- 迭代范围[Min, Max]并计算三个连续数字的频率的最大和。
- 最后,打印获得的最大和。
下面是上述方法的实现。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// 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)
{
// Stores the largest array element
int Max = *max_element(arr, arr + N);
// Stores the smallest array element
int Min = *min_element(arr, arr + N);
// freq[i]: Stores frequency of
// (i + Min) in the array
int freq[Max - Min + 1] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// Update frequency
// of arr[i]
freq[arr[i] - Min]++;
}
// Stores maximum frequency of an
// array element by incrementing
// or decrementing array elements
int maxSum = 0;
// Iterate all the numbers over
// the range [Min, Max]
for (int i = 0;
i < (Max - Min - 1); i++) {
// Stores sum of three
// consecutive numbers
int val = freq[i] + freq[i + 1] + freq[i + 2];
// Update maxSum
maxSum = max(maxSum, val);
}
// Print maxSum
cout << maxSum << "\n";
}
// 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{
// 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)
{
Arrays.sort(arr);
// Stores the largest array element
int Max = arr[N - 1];
// Stores the smallest array element
int Min = arr[0];
// freq[i]: Stores frequency of
// (i + Min) in the array
int freq[] = new int[Max - Min + 1];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update frequency
// of arr[i]
freq[arr[i] - Min]++;
}
// Stores maximum frequency of an
// array element by incrementing
// or decrementing array elements
int maxSum = 0;
// Iterate all the numbers over
// the range [Min, Max]
for(int i = 0; i < (Max - Min - 1); i++)
{
// Stores sum of three
// consecutive numbers
int val = freq[i] + freq[i + 1] +
freq[i + 2];
// Update maxSum
maxSum = Math.max(maxSum, val);
}
// Print maxSum
System.out.println(maxSum);
}
// 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 AnkThon
Python3
# Python3 program to implement
# the above approach
# Function to maximize the frequency
# of an array element by incrementing or
# decrementing array elements at most once
def max_freq(arr, N):
# Stores the largest array element
Max = max(arr)
# Stores the smallest array element
Min = min(arr)
# freq[i]: Stores frequency of
# (i + Min) in the array
freq = [0] * (Max - Min + 1)
# Traverse the array
for i in range(N):
# Update frequency
# of arr[i]
freq[arr[i] - Min] += 1
# Stores maximum frequency of an
# array element by incrementing
# or decrementing array elements
maxSum = 0
# Iterate all the numbers over
# the range [Min, Max]
for i in range( Max - Min - 1):
# Stores sum of three
# consecutive numbers
val = freq[i] + freq[i + 1] + freq[i + 2]
# Update maxSum
maxSum = max(maxSum, val)
# Print maxSum
print(maxSum)
# 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{
// 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)
{
Array.Sort(arr);
// Stores the largest array element
int Max = arr[N - 1];
// Stores the smallest array element
int Min = arr[0];
// freq[i]: Stores frequency of
// (i + Min) in the array
int[] freq = new int[Max - Min + 1];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update frequency
// of arr[i]
freq[arr[i] - Min]++;
}
// Stores maximum frequency of an
// array element by incrementing
// or decrementing array elements
int maxSum = 0;
// Iterate all the numbers over
// the range [Min, Max]
for(int i = 0; i < (Max - Min - 1); i++)
{
// Stores sum of three
// consecutive numbers
int val = freq[i] + freq[i + 1] +
freq[i + 2];
// Update maxSum
maxSum = Math.Max(maxSum, val);
}
// Print maxSum
Console.WriteLine(maxSum);
}
// 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 code_hunt.
Javascript
4
时间复杂度: O(N + |Max – Min|),其中Max、Min分别表示最大和最小的数组元素
辅助空间: O(|Max – Min|)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live