Maximum elements that can be made equal with k updates

Last Updated : 16 Mar, 2025

Given an array of integers and an integer k, the task is to determine the maximum number of equal elements that can be achieved in the array by incrementing its elements. The total number of increments performed across all elements must not exceed k.

Examples:

Input : arr = [ 2, 4, 9 ], k = 3 
Output :
Explanation: We are allowed to do at most three increments. We can make two elements 4 by increasing 2 by 2. Note that we can not make two elements 9 as converting 4 to 9 requires 5 increments.

Input : arr = [ 1, 2, 3, 4], k = 3
Output : 3
Explanation: You can increment the first element by 2 (to make it 3) and the second element by 1 (to make it 3). This results in the array [3,3,3,4], where 3 elements are equal. 

[Naive Approach] Using Recursion - O(n^2) Time

In the naive approach, we check for each element how many other elements can be incremented with the given limit on total increments so that they will become equal to the current element.

[Better Approach] Using Sorting - O(n^2) time and O(1) space

The idea is to sort the array and, for each element at index i, iterate backward from i to the start of the array, incrementing elements to match arr[i] while ensuring the total increments used do not exceed k. By calculating the number of elements that can be made equal to arr[i] within the limit k, we determine the maximum number of equal elements achievable for each i, and finally return the maximum count across all i.

We sort the array so that for each element in the array, the values closest to the current element are incremented. Thus, we can maximize the value count by minimizing the use of k.

Step by step approach:

  1. Sort the array in ascending order.
  2. For each element at index i, iterate backward from i to 0, incrementing elements to match arr[i] while keeping total increments ≤ k.
  3. Track the number of elements that can be made equal to arr[i] within the increment limit k.
  4. Update the maximum count of equal elements found during the process.
  5. Return the maximum count of equal elements after processing the entire array.
C++
// C++ program to find the Maximum elements 
// that can be made equal with k updates
#include <bits/stdc++.h>
using namespace std;

int maxEqual(vector<int> &arr, int k) {
    
    int ans = 0, n = arr.size();
    
    // Sort the array to make sure
    // closest smaller values are 
    // incremented first.
    sort(arr.begin(), arr.end());
    
    // For each value in arr 
    for (int i=0; i<n; i++) {
        
        int j = i;
        int kUsed = 0;
        
        // Increment arr[j] to arr[i]
        while (j>=0 && arr[i]-arr[j] <= k - kUsed) {
            kUsed += (arr[i]-arr[j]);
            j--;
        }
        
        // Number of equal elements will 
        // be (i-j);
        ans = max(ans, i-j);
    }
    
    return ans;
}

int main() {
    vector<int> arr = {1, 2, 3, 4};
    int k = 3;
    
    cout << maxEqual(arr, k);

    return 0;
}
Java
// Java program to find the Maximum elements 
// that can be made equal with k updates
import java.util.Arrays;

class GfG {

    static int maxEqual(int[] arr, int k) {
        
        int ans = 0, n = arr.length;
        
        // Sort the array to make sure
        // closest smaller values are 
        // incremented first.
        Arrays.sort(arr);
        
        // For each value in arr 
        for (int i = 0; i < n; i++) {
            
            int j = i;
            int kUsed = 0;
            
            // Increment arr[j] to arr[i]
            while (j >= 0 && arr[i] - arr[j] <= k - kUsed) {
                kUsed += (arr[i] - arr[j]);
                j--;
            }
            
            // Number of equal elements will 
            // be (i - j);
            ans = Math.max(ans, i - j);
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int k = 3;
        
        System.out.println(maxEqual(arr, k));
    }
}
Python
# Python program to find the Maximum elements 
# that can be made equal with k updates

def maxEqual(arr, k):
    
    ans = 0
    n = len(arr)
    
    # Sort the array to make sure
    # closest smaller values are 
    # incremented first.
    arr.sort()
    
    # For each value in arr 
    for i in range(n):
        
        j = i
        kUsed = 0
        
        # Increment arr[j] to arr[i]
        while j >= 0 and arr[i] - arr[j] <= k - kUsed:
            kUsed += (arr[i] - arr[j])
            j -= 1
        
        # Number of equal elements will 
        # be (i - j);
        ans = max(ans, i - j)
    
    return ans

if __name__ == "__main__":
    arr = [1, 2, 3, 4]
    k = 3
    
    print(maxEqual(arr, k))
C#
// C# program to find the Maximum elements 
// that can be made equal with k updates
using System;

class GfG {

    static int maxEqual(int[] arr, int k) {
        
        int ans = 0, n = arr.Length;
        
        // Sort the array to make sure
        // closest smaller values are 
        // incremented first.
        Array.Sort(arr);
        
        // For each value in arr 
        for (int i = 0; i < n;