给定一个由N 个正整数和一个整数K组成的数组arr[] ,任务是在执行恰好 K 次增量后找到任何数组元素的最高频率。
例子:
Input: arr[] = {1, 3, 2, 2}, K = 2
Output: 3
Explanation:
Below are the operations performed:
- Add 1 to the element at index 2(= 2), then the array modifies to {1, 3, 3, 2}.
- Add 1 to the element at index 3(= 2), then the array modifies to {1, 3, 3, 3}.
After the above steps, the maximum frequency of an array element is 3.
Input: arr[] = {4, 3, 4}, K = 5
Output: 2
方法:给定的问题可以通过使用滑动窗口技术和排序来解决。请按照以下步骤解决此问题:
- 将变量初始化为start 、 end 、 sum为0 ,而mostFreq为 INT_MIN。
- 按升序对数组arr[]进行排序。
- 使用变量end迭代范围[0, N – 1]并执行以下步骤:
- 将sum的值增加arr[end] 。
- 而(总和+ K)的值小于值(ARR [结束] *(结束-开始+ 1)),然后递减通过ARR之和的值[开始]和递增1开始的值。
- 将mostFreq的值更新为mostFreq和(end – start + 1)的最大值。
- 初始化一个变量,比如reqSum作为(arr[N-1] * N – sum) 的值,它存储结果和以使所有数组元素相等。
- 如果mostFreq 的值为N且K的值大于reqSum ,则将K的值递减reqSum 。
- 如果K的值是N的倍数,则打印N 。否则,打印(N – 1)的值。
- 完成上述步骤后,打印mostFreq的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the highest frequency
// of any array element possible by
// exactly K increment operations
void findMostFrequent(int arr[], int N,
int K)
{
int start = 0, end = 0;
// Sort the given array
sort(arr, arr + N);
// Stores the maximum frequency
// and the sum of sliding window
int mostFreq = INT_MIN, sum = 0;
// Traverse the array arr[]
for (end = 0; end < N; end++) {
// Add the current element
// to the window
sum = sum + arr[end];
// Decreasing the window size
while (sum + K < arr[end] * (end - start + 1)) {
// Update the value of sum
// by subtracting arr[start]
sum = sum - arr[start];
// Increment the value
// of the start
start++;
}
// Update maximum window size
mostFreq = max(mostFreq,
end - start + 1);
}
// Stores the required sum to
// make all elements of arr[] equal
int reqSum = arr[N - 1] * N - sum;
// If result from at most K increments
// is N and K is greater than reqSum
if (mostFreq == N && reqSum < K) {
// Decrement the value of K
// by reqSum
K = K - reqSum;
// If K is mutilpe of N then
// increment K/N times to
// every element
if (K % N == 0) {
cout << N << endl;
}
// Otherwise first make every
// element equal then increment
// remaining K to one element
else {
cout << N - 1 << endl;
}
return;
}
// Print the answer
cout << mostFreq << endl;
}
// Driver Code
int main()
{
int arr[] = { 4, 3, 4 };
int K = 5;
int N = sizeof(arr) / sizeof(arr[0]);
findMostFrequent(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the highest frequency
// of any array element possible by
// exactly K increment operations
static void findMostFrequent(int arr[], int N,
int K)
{
int start = 0, end = 0;
// Sort the given array
Arrays.sort(arr);
// Stores the maximum frequency
// and the sum of sliding window
int mostFreq = Integer.MIN_VALUE, sum = 0;
// Traverse the array arr[]
for (end = 0; end < N; end++) {
// Add the current element
// to the window
sum = sum + arr[end];
// Decreasing the window size
while (sum + K < arr[end] * (end - start + 1)) {
// Update the value of sum
// by subtracting arr[start]
sum = sum - arr[start];
// Increment the value
// of the start
start++;
}
// Update maximum window size
mostFreq = Math.max(mostFreq,
end - start + 1);
}
// Stores the required sum to
// make all elements of arr[] equal
int reqSum = arr[N - 1] * N - sum;
// If result from at most K increments
// is N and K is greater than reqSum
if (mostFreq == N && reqSum < K) {
// Decrement the value of K
// by reqSum
K = K - reqSum;
// If K is mutilpe of N then
// increment K/N times to
// every element
if (K % N == 0) {
System.out.println(N);
}
// Otherwise first make every
// element equal then increment
// remaining K to one element
else {
System.out.println(N - 1);
}
return;
}
// Print the answer
System.out.println( mostFreq);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 3, 4 };
int K = 5;
int N = arr.length;
findMostFrequent(arr, N, K);
}
}
// This code is contributed by target_2.
Python3
# Python program for the above approach
# Function to find the highest frequency
# of any array element possible by
# exactly K increment operations
def findMostFrequent( arr, N, K):
start = 0
end = 0
# Sort the given array
arr.sort()
# Stores the maximum frequency
# and the sum of sliding window
mostFreq = -2**31
sum = 0
# Traverse the array arr[]
for end in range(N):
# Add the current element
# to the window
sum = sum + arr[end]
# Decreasing the window size
while (sum + K < arr[end] * (end - start + 1)):
# Update the value of sum
# by subtracting arr[start]
sum = sum - arr[start]
# Increment the value
# of the start
start += 1
# Update maximum window size
mostFreq = max(mostFreq, end - start + 1)
# Stores the required sum to
# make all elements of arr[] equal
reqSum = arr[N - 1] * N - sum
# If result from at most K increments
# is N and K is greater than reqSum
if (mostFreq == N and reqSum < K):
# Decrement the value of K
# by reqSum
K = K - reqSum
# If K is mutilpe of N then
# increment K/N times to
# every element
if (K % N == 0):
print(N)
# Otherwise first make every
# element equal then increment
# remaining K to one element
else:
print(N - 1)
return
# Print the answer
print(mostFreq)
# Driver Code
arr = [4, 3, 4]
K = 5
N = len(arr)
findMostFrequent(arr, N, K)
# This code is contributed by shubhamsingh10
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the highest frequency
// of any array element possible by
// exactly K increment operations
static void findMostFrequent(int []arr, int N,
int K)
{
int start = 0, end = 0;
// Sort the given array
Array.Sort(arr);
// Stores the maximum frequency
// and the sum of sliding window
int mostFreq = Int32.MinValue, sum = 0;
// Traverse the array arr[]
for(end = 0; end < N; end++)
{
// Add the current element
// to the window
sum = sum + arr[end];
// Decreasing the window size
while (sum + K < arr[end] * (end - start + 1))
{
// Update the value of sum
// by subtracting arr[start]
sum = sum - arr[start];
// Increment the value
// of the start
start++;
}
// Update maximum window size
mostFreq = Math.Max(mostFreq,
end - start + 1);
}
// Stores the required sum to
// make all elements of arr[] equal
int reqSum = arr[N - 1] * N - sum;
// If result from at most K increments
// is N and K is greater than reqSum
if (mostFreq == N && reqSum < K)
{
// Decrement the value of K
// by reqSum
K = K - reqSum;
// If K is mutilpe of N then
// increment K/N times to
// every element
if (K % N == 0)
{
Console.Write(N);
}
// Otherwise first make every
// element equal then increment
// remaining K to one element
else
{
Console.Write(N - 1);
}
return;
}
// Print the answer
Console.Write( mostFreq);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, 3, 4 };
int K = 5;
int N = arr.Length;
findMostFrequent(arr, N, K);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
2
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。