📌  相关文章
📜  最大化元素的频率,最多增加或减少所有数组元素的个数

📅  最后修改于: 2021-04-26 04:56:26             🧑  作者: 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.


输出:
4

时间复杂度: O(N + | Max – Min |),其中Max,Min分别表示最大和最小数组元素
辅助空间: O(| Max – Min |)