📌  相关文章
📜  最多通过所有数组元素的一个增量或减量来最大化元素的频率

📅  最后修改于: 2021-10-26 06:51:03             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过将每个数组元素递增或递减1至多一次来找到任何数组元素的最大频率。

例子:

方法:该问题可以使用贪心技术解决。这个想法是找到数组中存在的最大和最小元素,并通过迭代数组元素范围内的所有数字来计算三个连续数字的频率之和。最后,打印获得的最大和。请按照以下步骤解决问题:

  • 找到数组的最大元素,比如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|)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程