Maximum frequency of any array element possible by at most K increments - GeeksforGeeks

13 Jul, 2021
Given an array arr[] of size N and an integer K, the task is to find the maximum possible frequency of any array element by at most K increments.
Examples:
Input: arr[] = {1, 4, 8, 13}, N = 4, K = 5 Output: 2 Explanation: Incrementing arr[0] twice modifies arr[] to {4, 4, 8, 13}. Maximum frequency = 2. Incrementing arr[1] four times modifies arr[] to {1, 8, 8, 13}. Maximum frequency = 2. Incrementing arr[2] five times modifies arr[] to {1, 4, 13, 13}. Maximum frequency = 2. Therefore, the maximum possible frequency of any array element that can be obtained by at most 5 increments is 2.
Input: arr[] = {2, 4, 5}, N = 3, K = 4 Output: 3
Approach: This problem can be solved by using Sliding Window Technique and Sorting. Follow the steps to solve this problem.
Sort the array arr[].
Initialize variables sum = 0, start = 0 and resultant frequency res = 0.
Traverse the array over the range of indices [0, N – 1] and perform the following steps:Increment sum by arr[end].Iterate a loop until the value of [(end – start + 1) * arr[end] – sum] is less than K and perform the following operatiosn: Decrement the value of sum by arr[start].Increment the value of start by 1.After completing the above steps, all the elements over the range [start, end] can be made equal by using at most K operations. Therefore, update the value of res as the maximum of res and (end – start + 1).
Increment sum by arr[end].
Iterate a loop until the value of [(end – start + 1) * arr[end] – sum] is less than K and perform the following operatiosn: Decrement the value of sum by arr[start].Increment the value of start by 1.
Decrement the value of sum by arr[start].
Increment the value of start by 1.
After completing the above steps, all the elements over the range [start, end] can be made equal by using at most K operations. Therefore, update the value of res as the maximum of res and (end – start + 1).
Finally, print the value of res as frequency of most frequent element after performing Koperations.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the maximum possible// frequency of a most frequent element// after at most K increment operationsvoid 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 codeint main(){    int arr[] = { 1, 4, 8, 13 };    int N = 4;    int K = 5;    maxFrequency(arr, N, K);    return 0;}
// Java program for the above approachimport java.util.Arrays; class GFG{ // Function to find the maximum possible// frequency of a most frequent element// after at most K increment operationsstatic 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 codepublic 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
# Python3 program for the above approach # Function to find the maximum possible# frequency of a most frequent element# after at most K increment operationsdef maxFrequency(arr, N, K):         # Sort the input array    arr.sort()     start = 0    end = 0     # Stores the sum of sliding    # window and the maximum possible    # frequency of any array element    sum = 0    res = 0     # Traverse the array    for end in range(N):         # 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 += 1         # Update the maximum possible frequency        res = max(res, end - start + 1)     # Print the frequency of    # the most frequent array    # element after K increments    print(res) # Driver codeif __name__ == '__main__':         arr = [ 1, 4, 8, 13 ]    N = 4    K = 5         maxFrequency(arr, N, K)     # This code is contributed by ipg2016107
// C# program for the above approachusing System;         class GFG{ // Function to find the maximum possible// frequency of a most frequent element// after at most K increment operationsstatic 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 Codepublic 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
<script> // JavaScript program for the above approach  // Function to find the maximum possible// frequency of a most frequent element// after at most K increment operationsfunction maxFrequency(arr, N, K) {    // Sort the input array    arr.sort((a, b) => a - b);     let start = 0, end = 0;     // Stores the sum of sliding    // window and the maximum possible    // frequency of any array element    let 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    document.write(res + "<br>");} // Driver code let arr = [1, 4, 8, 13];let N = 4;let K = 5;maxFrequency(arr, N, K); </script>
2
 
Time Complexity: O(NlogN) Auxiliary Space: O(1)
abhinavjain194
code_hunt
ipg2016107
_saurabh_jaiswal
frequency-counting
Picked
sliding-window
Arrays
Mathematical
sliding-window
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in Java
Queue | Set 1 (Introduction and Array Implementation)
Linear Search
Python | Using 2D arrays/lists the right way
Maximum and minimum of an array using minimum number of comparisons
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7