Maximum elements that can be made equal with k updates | Set 2
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++
#include <bits/stdc++.h>
using namespace std;
void getMax(vector< int >& nums, int k)
{
int n = nums.size();
sort(nums.begin(), nums.end());
long sum = 0;
int ans = 0;
int i, j;
i = j = 0;
for (j = 0; j < n; j++) {
sum += nums[j];
if (( long )(sum + k)
>= (( long )nums[j] * (j - i + 1)))
continue ;
if ((j - i) > ans)
ans = j - i;
sum -= nums[i];
i++;
}
ans = max(ans, j - i);
cout << ans << endl;
}
int main()
{
vector< int > nums = { 1, 3, 4 };
int k = 6;
getMax(nums, k);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static void getMax( int nums[ ], int k)
{
int n = nums.length;
Arrays.sort(nums);
long sum = 0 ;
int ans = 0 ;
int i, j;
i = j = 0 ;
for (j = 0 ; j < n; j++) {
sum += nums[j];
if (( long )(sum + k)
>= (( long )nums[j] * (j - i + 1 )))
continue ;
if ((j - i) > ans)
ans = j - i;
sum -= nums[i];
i++;
}
ans = Math.max(ans, j - i);
System.out.println(ans);
}
public static void main (String[] args)
{
int nums[ ] = { 1 , 3 , 4 };
int k = 6 ;
getMax(nums, k);
}
}
|
Python
def getMax(nums, k):
n = len (nums)
nums.sort()
sum = 0
ans = 0
i = j = f = 0
for j in range (n):
sum = sum + nums[j]
if (( sum + k) > = (nums[j] * (j - i + 1 ))):
f = 1
continue
if ((j - i) > ans):
ans = j - i
sum = sum - nums[i]
i = i + 1
f = 1
if (f = = 1 ):
ans = max (ans, j - i + 1 )
else :
ans = max (ans, j - i)
print (ans)
nums = [ 1 , 3 , 4 ]
k = 6
getMax(nums, k)
|
C#
using System;
class GFG
{
static void getMax( int [] nums, int k)
{
int n = nums.Length;
Array.Sort(nums);
long sum = 0;
int ans = 0;
int i, j;
i = j = 0;
for (j = 0; j < n; j++)
{
sum += nums[j];
if (( long )(sum + k)
>= (( long )nums[j] * (j - i + 1)))
continue ;
if ((j - i) > ans)
ans = j - i;
sum -= nums[i];
i++;
}
ans = Math.Max(ans, j - i);
Console.Write(ans);
}
public static void Main()
{
int [] nums = { 1, 3, 4 };
int k = 6;
getMax(nums, k);
}
}
|
Javascript
<script>
function getMax(nums, k)
{
let n = nums.length;
nums.sort( function (a, b) { return a - b })
let sum = 0;
let ans = 0;
let i, j;
i = j = 0;
for (j = 0; j < n; j++) {
sum += nums[j];
if ((sum + k)
>= (nums[j] * (j - i + 1)))
continue ;
if ((j - i) > ans)
ans = j - i;
sum -= nums[i];
i++;
}
ans = Math.max(ans, j - i);
document.write(ans + '<br>')
}
let nums = [1, 3, 4];
let k = 6;
getMax(nums, k);
</script>
|
Time Complexity: O(N*log(N)), where N is the size of array nums
Auxiliary Space: O(1)
Last Updated :
27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...