Given an array and a value k. We have to find the maximum number of equal elements possible for the array so that we can increase the elements of the array by incrementing a total of at-most k.
Examples:
Input : array = { 2, 4, 9 }, k = 3
Output : 2
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 : array = { 5, 5, 3, 1 }, k = 5
Output : 3
Explanation: Here 1st and 2nd elements are equal. Then we can increase 3rd element 3 upto 5. Then k becomes (k-2) = 3. Now we can’t increase 1 to 5 because k value is 3 and we need 4 for the updation. Thus equal elements possible are 3. Here we can also increase 1 to 5. Then also we have 3 because we can’t update 3 to 5.Input : array = { 5, 5, 3, 1 }, k = 6
Output : 4
Naive Approach: In the naive approach we have an algorithm in O(n^2) time in which we check for each element how many other elements can be incremented so that they will become equal to them.
Efficient Approach: In this approach, first we will sort the array. Then we maintain two arrays. First is prefix sum array which stores the prefix sum of the array and another is maxx[] array which stores the maximum element found till every point, i.e., max[i] means maximum element from 1 to i. After storing these values in prefix[] array and maxx[] array, we do the binary search from 1 to n(number of elements of the array) to calculate how many elements which can be incremented to make them equal. In the binary search, we use one function in which we determine what is the number of elements can be incremented to make them equal to a single value.
C++
// C++ program to find maximum elements that can // be made equal with k updates #include <bits/stdc++.h> using namespace std; // Function to calculate the maximum number of // equal elements possible with atmost K increment // of values .Here we have done sliding window // to determine that whether there are x number of // elements present which on increment will become // equal. The loop here will run in fashion like // 0...x-1, 1...x, 2...x+1, ...., n-x-1...n-1 bool ElementsCalculationFunc(int pre[], int maxx[], int x, int k, int n) { for (int i = 0, j = x; j <= n; j++, i++) { // It can be explained with the reasoning // that if for some x number of elements // we can update the values then the // increment to the segment (i to j having // length -> x) so that all will be equal is // (x*maxx[j]) this is the total sum of // segment and (pre[j]-pre[i]) is present sum // So difference of them should be less than k // if yes, then that segment length(x) can be // possible return true if (x * maxx[j] - (pre[j] - pre[i]) <= k) return true; } return false; } void MaxNumberOfElements(int a[], int n, int k) { // sort the array in ascending order sort(a, a + n); int pre[n + 1]; // prefix sum array int maxx[n + 1]; // maximum value array // Initializing the prefix array // and maximum array for (int i = 0; i <= n; ++i) { pre[i] = 0; maxx[i] = 0; } // set the first element of both // array maxx[0] = a[0]; pre[0] = a[0]; for (int i = 1; i < n; i++) { // Calculating prefix sum of the array pre[i] = pre[i - 1] + a[i]; // Calculating max value upto that position // in the array maxx[i] = max(maxx[i - 1], a[i]); } // Binary seach applied for // computation here int l = 1, r = n, ans; while (l < r) { int mid = (l + r) / 2; if (ElementsCalculationFunc(pre, maxx, mid - 1, k, n)) { ans = mid; l = mid + 1; } else r = mid - 1; } // printing result cout << ans << "\n"; } int main() { int arr[] = { 2, 4, 9 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; MaxNumberOfElements(arr, n, k); return 0; } |
Java
// java program to find maximum elements that can // be made equal with k updates import java.util.Arrays; public class GFG { // Function to calculate the maximum number of // equal elements possible with atmost K increment // of values .Here we have done sliding window // to determine that whether there are x number of // elements present which on increment will become // equal. The loop here will run in fashion like // 0...x-1, 1...x, 2...x+1, ...., n-x-1...n-1 static boolean ElementsCalculationFunc(int pre[], int maxx[], int x, int k, int n) { for (int i = 0, j = x; j <= n; j++, i++) { // It can be explained with the reasoning // that if for some x number of elements // we can update the values then the // increment to the segment (i to j having // length -> x) so that all will be equal is // (x*maxx[j]) this is the total sum of // segment and (pre[j]-pre[i]) is present sum // So difference of them should be less than k // if yes, then that segment length(x) can be // possible return true if (x * maxx[j] - (pre[j] - pre[i]) <= k) return true; } return false; } static void MaxNumberOfElements(int a[], int n, int k) { // sort the array in ascending order Arrays.sort(a); int []pre = new int[n + 1]; // prefix sum array int []maxx = new int[n + 1]; // maximum value array // Initializing the prefix array // and maximum array for (int i = 0; i <= n; ++i) { pre[i] = 0; maxx[i] = 0; } // set the first element of both // array maxx[0] = a[0]; pre[0] = a[0]; for (int i = 1; i < n; i++) { // Calculating prefix sum of the array pre[i] = pre[i - 1] + a[i]; // Calculating max value upto that position // in the array maxx[i] = Math.max(maxx[i - 1], a[i]); } // Binary seach applied for // computation here int l = 1, r = n, ans=0; while (l < r) { int mid = (l + r) / 2; if (ElementsCalculationFunc(pre, maxx, mid - 1, k, n)) { ans = mid; l = mid + 1; } else r = mid - 1; } // printing result System.out.print((int)ans + "\n"); } public static void main(String args[]) { int arr[] = { 2, 4, 9 }; int n = arr.length; int k = 3; MaxNumberOfElements(arr, n, k); } } // This code is contributed by Sam007 |
Python3
# Python3 program to find maximum elements # that can be made equal with k updates # Function to calculate the maximum number of # equal elements possible with atmost K increment # of values .Here we have done sliding window # to determine that whether there are x number of # elements present which on increment will become # equal. The loop here will run in fashion like # 0...x-1, 1...x, 2...x+1, ...., n-x-1...n-1 def ElementsCalculationFunc(pre, maxx, x, k, n): i = 0 j = x while j <= n: # It can be explained with the reasoning # that if for some x number of elements # we can update the values then the # increment to the segment (i to j having # length -> x) so that all will be equal is # (x*maxx[j]) this is the total sum of # segment and (pre[j]-pre[i]) is present sum # So difference of them should be less than k # if yes, then that segment length(x) can be # possible return true if (x * maxx[j] - (pre[j] - pre[i]) <= k): return True i += 1 j += 1 return False def MaxNumberOfElements( a, n, k): # sort the array in ascending order a.sort() pre = [0] * (n + 1) # prefix sum array maxx = [0] * (n + 1) # maximum value array # Initializing the prefix array # and maximum array for i in range (n + 1): pre[i] = 0 maxx[i] = 0 # set the first element of both # array maxx[0] = a[0] pre[0] = a[0] for i in range(1, n): # Calculating prefix sum of the array pre[i] = pre[i - 1] + a[i] # Calculating max value upto that # position in the array maxx[i] = max(maxx[i - 1], a[i]) # Binary seach applied for # computation here l = 1 r = n while (l < r) : mid = (l + r) // 2 if (ElementsCalculationFunc(pre, maxx, mid - 1, k, n)): ans = mid l = mid + 1 else: r = mid - 1 # printing result print (ans) # Driver Code if __name__ == "__main__": arr = [2, 4, 9 ] n = len(arr) k = 3 MaxNumberOfElements(arr, n, k) # This code is contributed by Ita_c |
C#
// C# program to find maximum elements that can // be made equal with k updates using System; class GFG { // Function to calculate the maximum number of // equal elements possible with atmost K increment // of values .Here we have done sliding window // to determine that whether there are x number of // elements present which on increment will become // equal. The loop here will run in fashion like // 0...x-1, 1...x, 2...x+1, ...., n-x-1...n-1 static bool ElementsCalculationFunc(int []pre, int []maxx, int x, int k, int n) { for (int i = 0, j = x; j <= n; j++, i++) { // It can be explained with the reasoning // that if for some x number of elements // we can update the values then the // increment to the segment (i to j having // length -> x) so that all will be equal is // (x*maxx[j]) this is the total sum of // segment and (pre[j]-pre[i]) is present sum // So difference of them should be less than k // if yes, then that segment length(x) can be // possible return true if (x * maxx[j] - (pre[j] - pre[i]) <= k) return true; } return false; } static void MaxNumberOfElements(int []a, int n, int k) { // sort the array in ascending order Array.Sort(a); int []pre = new int[n + 1]; // prefix sum array int []maxx = new int[n + 1]; // maximum value array // Initializing the prefix array // and maximum array for (int i = 0; i <= n; ++i) { pre[i] = 0; maxx[i] = 0; } // set the first element of both // array maxx[0] = a[0]; pre[0] = a[0]; for (int i = 1; i < n; i++) { // Calculating prefix sum of the array pre[i] = pre[i - 1] + a[i]; // Calculating max value upto that position // in the array maxx[i] = Math.Max(maxx[i - 1], a[i]); } // Binary seach applied for // computation here int l = 1, r = n, ans=0; while (l < r) { int mid = (l + r) / 2; if (ElementsCalculationFunc(pre, maxx, mid - 1, k, n)) { ans = mid; l = mid + 1; } else r = mid - 1; } // printing result Console.Write ((int)ans + "\n"); } // Driver code public static void Main() { int []arr = { 2, 4, 9 }; int n = arr.Length; int k = 3; MaxNumberOfElements(arr, n, k); } } // This code is contributed by Sam007 |
PHP
<?php //PHP program to find maximum elements that can // be made equal with k updates // Function to calculate the maximum number of // equal elements possible with atmost K increment // of values .Here we have done sliding window // to determine that whether there are x number of // elements present which on increment will become // equal. The loop here will run in fashion like // 0...x-1, 1...x, 2...x+1, ...., n-x-1...n-1 function ElementsCalculationFunc($pre, $maxx,$x, $k, $n) { for ( $i = 0, $j = $x; $j <= $n; $j++, $i++) { // It can be explained with the reasoning // that if for some x number of elements // we can update the values then the // increment to the segment (i to j having // length -> x) so that all will be equal is // (x*maxx[j]) this is the total sum of // segment and (pre[j]-pre[i]) is present sum // So difference of them should be less than k // if yes, then that segment length(x) can be // possible return true if ($x * $maxx[$j] - ($pre[$j] - $pre[$i]) <= $k) return true; } return false; } function MaxNumberOfElements($a, $n, $k) { // sort the array in ascending order sort($a); $pre[$n + 1]=array(); // prefix sum array $maxx[$n + 1]=array(); // maximum value array // Initializing the prefix array // and maximum array for ($i = 0; $i <= $n; ++$i) { $pre[$i] = 0; $maxx[$i] = 0; } // set the first element of both // array $maxx[0] = $a[0]; $pre[0] = $a[0]; for ($i = 1; $i < $n; $i++) { // Calculating prefix sum of the array $pre[$i] = $pre[$i - 1] + $a[$i]; // Calculating max value upto that position // in the array $maxx[$i] = max($maxx[$i - 1], $a[$i]); } // Binary seach applied for // computation here $l = 1; $r = $n; $ans; while ($l < $r) { $mid = ($l + $r) / 2; if (ElementsCalculationFunc($pre, $maxx, $mid - 1, $k, $n)) { $ans = $mid; $l = $mid + 1; } else $r = $mid - 1; } // printing result echo $ans , "\n"; } //Code driven $arr = array(2, 4, 9 ); $n = sizeof($arr) / sizeof($arr[0]); $k = 3; MaxNumberOfElements($arr, $n, $k); #This code is contributed by akt_mit. ?> |
2
Time Complexity :O(nlog(n))
Space Complexity : O(n)
Recommended Posts:
- Longest subarray whose elements can be made equal by maximum K increments
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Queries to count array elements greater than or equal to a given number with updates
- Check if two arrays can be made equal by reversing any subarray once
- Sum and Maximum of elements in array from [L, R] before and after updates
- Maximum number of parallelograms that can be made using the given length of line segments
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Queries to find maximum product pair in range with updates
- Array range queries to find the maximum Fibonacci number with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Array range queries to find the number of perfect square elements with updates
- XOR of elements in a given range with updates using Fenwick Tree
- Queries for count of array elements with values in given range with updates
- Find if neat arrangement of cups and shelves can be made
- Check if string can be made lexicographically smaller by reversing any substring
- Check if the rows of a binary matrix can be made unique by removing a single column
- Check if all strings of an array can be made same by interchanging characters
- Send SMS updates to mobile phone using python
- Number of primes in a subarray (with updates)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


