Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 13 Jul, 2021
Improve Article
Save Article

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:
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).
  • Finally, print the value of res as frequency of most frequent element after performing Koperations.

Below is the implementation of the above approach:

C++




// 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 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

Python3




# Python3 program for the above approach
 
# Function to find the maximum possible
# frequency of a most frequent element
# after at most K increment operations
def 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 code
if __name__ == '__main__':
     
    arr = [ 1, 4, 8, 13 ]
    N = 4
    K = 5
     
    maxFrequency(arr, N, K)
     
# This code is contributed by ipg2016107

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

Javascript




<script>
 
// JavaScript program for the above approach
 
 
// Function to find the maximum possible
// frequency of a most frequent element
// after at most K increment operations
function 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>

Output: 

2

 

Time Complexity: O(NlogN) 
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!