📌  相关文章
📜  任何数组元素可能的最大频率最多为 K 个增量

📅  最后修改于: 2021-09-05 11:37:24             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是通过最多K 个增量找到任何数组元素的最大可能频率。

例子:

方法:这个问题可以通过使用滑动窗口技术和排序来解决。请按照以下步骤解决此问题。

  • 对数组arr[] 进行排序。
  • 初始化变量sum = 0, start = 0和结果频率res = 0
  • 在索引范围[0, N – 1] 上遍历数组并执行以下步骤:
    • 通过arr[end]增加总和
    • 迭代一个循环,直到[(end – start + 1) * arr[end] – sum] 的值小于K并执行以下操作:
      • 通过arr[start]减少sum的值。
      • start的值增加1
    • 完成上述步骤后,最多可以使用K 次操作,使[start, end]范围内的所有元素相等。因此,将res的值更新为 res 和 (end – start + 1)最大值。
  • 最后,在执行K 次操作后,将res的值打印为最频繁元素的频率。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum possible
// frequency of a most frequent element
// after at most K increment operations
void maxFrequency(int arr[], int N, int K)
{
    // Sort the input array
    sort(arr, arr + N);
 
    int start = 0, end = 0;
 
    // Stores the sum of sliding
    // window and the maximum possible
    // frequency of any array element
    int sum = 0, res = 0;
 
    // Traverse the array
    for (end = 0; end < N; end++) {
 
        // Add the current element
        // to the window
        sum += arr[end];
 
        // Decrease the window size
 
        // If it is not possible to make the
        // array elements in the window equal
        while ((end - start + 1) * arr[end] - sum > K) {
 
            // Update the value of sum
            sum -= arr[start];
 
            // Increment the value of start
            start++;
        }
 
        // Update the maximum possible frequency
        res = max(res, end - start + 1);
    }
 
    // Print the frequency of
    // the most frequent array
    // element after K increments
    cout << res << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 4, 8, 13 };
    int N = 4;
    int K = 5;
    maxFrequency(arr, N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG{
 
// Function to find the maximum possible
// frequency of a most frequent element
// after at most K increment operations
static void maxFrequency(int arr[], int N, int K)
{
     
    // Sort the input array
    Arrays.sort(arr);
 
    int start = 0, end = 0;
 
    // Stores the sum of sliding
    // window and the maximum possible
    // frequency of any array element
    int sum = 0, res = 0;
 
    // Traverse the array
    for(end = 0; end < N; end++)
    {
         
        // Add the current element
        // to the window
        sum += arr[end];
 
        // Decrease the window size
 
        // If it is not possible to make the
        // array elements in the window equal
        while ((end - start + 1) *
                   arr[end] - sum > K)
        {
             
            // Update the value of sum
            sum -= arr[start];
 
            // Increment the value of start
            start++;
        }
 
        // Update the maximum possible frequency
        res = Math.max(res, end - start + 1);
    }
 
    // Print the frequency of
    // the most frequent array
    // element after K increments
    System.out.println(res);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 4, 8, 13 };
    int N = 4;
    int K = 5;
     
    maxFrequency(arr, N, K);
}
}
 
// This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
         
class GFG{
 
// Function to find the maximum possible
// frequency of a most frequent element
// after at most K increment operations
static void maxFrequency(int[] arr, int N, int K)
{
     
    // Sort the input array
    Array.Sort(arr);
 
    int start = 0, end = 0;
 
    // Stores the sum of sliding
    // window and the maximum possible
    // frequency of any array element
    int sum = 0, res = 0;
 
    // Traverse the array
    for(end = 0; end < N; end++)
    {
         
        // Add the current element
        // to the window
        sum += arr[end];
 
        // Decrease the window size
 
        // If it is not possible to make the
        // array elements in the window equal
        while ((end - start + 1) *
           arr[end] - sum > K)
        {
             
            // Update the value of sum
            sum -= arr[start];
 
            // Increment the value of start
            start++;
        }
 
        // Update the maximum possible frequency
        res = Math.Max(res, end - start + 1);
    }
 
    // Print the frequency of
    // the most frequent array
    // element after K increments
    Console.WriteLine(res);
}
     
// Driver Code
public static void Main()
{
    int[] arr = { 1, 4, 8, 13 };
    int N = 4;
    int K = 5;
     
    maxFrequency(arr, N, K);
}
}
 
// This code is contributed by code_hunt


输出:
2

时间复杂度: O(NlogN)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live