Given an array nums[] and a value k. The task is to find the maximum elements that can be made equal with at most k updates/increments.
Examples:
Input : nums[] = { 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 : nums[] = { 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 : nums[] = { 5, 5, 3, 1 }, k = 6
Output : 4
Approach: This is a space optimized approach of the efficient approach discussed in Set 1 of the article.
The task can be solved with the help of the sliding window concept. Basically for each window from (i till j), we see whether all the elements of the current window can be made equal to the last element of the window. If it’s possible in at most k updates, then store the size of the window, else discard the starting element of the window.
Follow the below steps to solve the problem:
- Sort the array nums.
- Declare an integer variable sum to store the running sum of array elements.
- Declare an integer variable ans to store the maximum possible frequency of the most frequent element in nums array.
- Let i…j be the current window we are processing.
- Traverse the array nums.
- Basically, at each step, we are trying to make all the elements from nums[i] to nums[j] equal to nums[j].
- If the sum of all elements from nums[i] to nums[j] plus the value of k is enough to do so then the window is valid.
- Else, the value of i needs to be incremented because the difference of values of nums[i] and nums[j] is maximum, so nums[i] takes the maximum part of k to become equal to nums[j], that’s why it needs to be removed from the current window.
- The value of ans is equal to the size of the current valid window i.e. (j-i)
- Print the frequency of the most frequent element in array nums.
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 count of // equal elements void getMax(vector< int >& nums, int k) { // Size of nums array int n = nums.size(); // Sort the nums array sort(nums.begin(), nums.end()); // Stores the running sum // of array elements long sum = 0; // Stores the maximum possible frequency int ans = 0; // i is the starting index of the window // j is the ending index of the window int i, j; i = j = 0; // Traverse the array nums for (j = 0; j < n; j++) { // Add the value of // current element to sum sum += nums[j]; // If the addition of sum // and k is sufficient to // make all the array elements // from i..j equal to nums[j] if (( long )(sum + k) >= (( long )nums[j] * (j - i + 1))) continue ; // Update the value of ans // to store the maximum // possible frequency so far if ((j - i) > ans) ans = j - i; // Subtract the value of nums[i] from sum, // because the addition of sum and // k are not sufficient to make // all the array elements from i..j // equal to nums[j] and difference of // values of A[j] and A[i] is maximum, // so A[i] takes the maximum part of k // to become equal to A[j], // that's why it is removed // from current window. sum -= nums[i]; // Slide the current window i++; } // Update the value of ans if required ans = max(ans, j - i); // Print the result cout << ans << endl; } // Driver Code int main() { vector< int > nums = { 1, 3, 4 }; int k = 6; getMax(nums, k); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find the maximum count of // equal elements static void getMax( int nums[ ], int k) { // Size of nums array int n = nums.length; // Sort the nums array Arrays.sort(nums); // Stores the running sum // of array elements long sum = 0 ; // Stores the maximum possible frequency int ans = 0 ; // i is the starting index of the window // j is the ending index of the window int i, j; i = j = 0 ; // Traverse the array nums for (j = 0 ; j < n; j++) { // Add the value of // current element to sum sum += nums[j]; // If the addition of sum // and k is sufficient to // make all the array elements // from i..j equal to nums[j] if (( long )(sum + k) >= (( long )nums[j] * (j - i + 1 ))) continue ; // Update the value of ans // to store the maximum // possible frequency so far if ((j - i) > ans) ans = j - i; // Subtract the value of nums[i] from sum, // because the addition of sum and // k are not sufficient to make // all the array elements from i..j // equal to nums[j] and difference of // values of A[j] and A[i] is maximum, // so A[i] takes the maximum part of k // to become equal to A[j], // that's why it is removed // from current window. sum -= nums[i]; // Slide the current window i++; } // Update the value of ans if required ans = Math.max(ans, j - i); // Print the result System.out.println(ans); } public static void main (String[] args) { int nums[ ] = { 1 , 3 , 4 }; int k = 6 ; getMax(nums, k); } } // This code is contributed by hrithikgarg03188 |
Python
# Python program for the above approach # Function to find the maximum count of # equal elements def getMax(nums, k): # Size of nums array n = len (nums) # Sort the nums array nums.sort() # Stores the running sum # of array elements sum = 0 # Stores the maximum possible frequency ans = 0 # i is the starting index of the window # j is the ending index of the window i = j = f = 0 # Traverse the array nums for j in range (n): # Add the value of # current element to sum sum = sum + nums[j] # If the addition of sum # and k is sufficient to # make all the array elements # from i..j equal to nums[j] if (( sum + k) > = (nums[j] * (j - i + 1 ))): f = 1 continue # Update the value of ans # to store the maximum # possible frequency so far if ((j - i) > ans): ans = j - i # Subtract the value of nums[i] from sum, # because the addition of sum and # k are not sufficient to make # all the array elements from i..j # equal to nums[j] and difference of # values of A[j] and A[i] is maximum, # so A[i] takes the maximum part of k # to become equal to A[j], # that's why it is removed # from current window. sum = sum - nums[i] # Slide the current window i = i + 1 f = 1 # Update the value of ans if required if (f = = 1 ): ans = max (ans, j - i + 1 ) else : ans = max (ans, j - i) # Print the result print (ans) # Driver Code nums = [ 1 , 3 , 4 ] k = 6 getMax(nums, k) # This code is contributed by Samim Hossain Mondal. |
C#
// C# program for the above approach using System; class GFG { // Function to find the maximum count of // equal elements static void getMax( int [] nums, int k) { // Size of nums array int n = nums.Length; // Sort the nums array Array.Sort(nums); // Stores the running sum // of array elements long sum = 0; // Stores the maximum possible frequency int ans = 0; // i is the starting index of the window // j is the ending index of the window int i, j; i = j = 0; // Traverse the array nums for (j = 0; j < n; j++) { // Add the value of // current element to sum sum += nums[j]; // If the addition of sum // and k is sufficient to // make all the array elements // from i..j equal to nums[j] if (( long )(sum + k) >= (( long )nums[j] * (j - i + 1))) continue ; // Update the value of ans // to store the maximum // possible frequency so far if ((j - i) > ans) ans = j - i; // Subtract the value of nums[i] from sum, // because the addition of sum and // k are not sufficient to make // all the array elements from i..j // equal to nums[j] and difference of // values of A[j] and A[i] is maximum, // so A[i] takes the maximum part of k // to become equal to A[j], // that's why it is removed // from current window. sum -= nums[i]; // Slide the current window i++; } // Update the value of ans if required ans = Math.Max(ans, j - i); // Print the result Console.Write(ans); } public static void Main() { int [] nums = { 1, 3, 4 }; int k = 6; getMax(nums, k); } } // This code is contributed by Saurabh Jaiswal |
Javascript
<script> // JavaScript code for the above approach // Function to find the maximum count of // equal elements function getMax(nums, k) { // Size of nums array let n = nums.length; // Sort the nums array nums.sort( function (a, b) { return a - b }) // Stores the running sum // of array elements let sum = 0; // Stores the maximum possible frequency let ans = 0; // i is the starting index of the window // j is the ending index of the window let i, j; i = j = 0; // Traverse the array nums for (j = 0; j < n; j++) { // Add the value of // current element to sum sum += nums[j]; // If the addition of sum // and k is sufficient to // make all the array elements // from i..j equal to nums[j] if ((sum + k) >= (nums[j] * (j - i + 1))) continue ; // Update the value of ans // to store the maximum // possible frequency so far if ((j - i) > ans) ans = j - i; // Subtract the value of nums[i] from sum, // because the addition of sum and // k are not sufficient to make // all the array elements from i..j // equal to nums[j] and difference of // values of A[j] and A[i] is maximum, // so A[i] takes the maximum part of k // to become equal to A[j], // that's why it is removed // from current window. sum -= nums[i]; // Slide the current window i++; } // Update the value of ans if required ans = Math.max(ans, j - i); // Print the result document.write(ans + '<br>') } // Driver Code let nums = [1, 3, 4]; let k = 6; getMax(nums, k); // This code is contributed by Potta Lokesh </script> |
3
Time Complexity: O(N*log(N)), where N is the size of array nums
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!