Maximum elements that can be made equal with k updates in C++


Given the task is to find the maximum number of elements that can be made equal in a given array after incrementing its elements by at-most k times.

Let’s now understand what we have to do using an example −

Input

a[] = {1, 3, 8}, k = 4

Output

2

Explanation

Here we can obtain two fours by incrementing 1 three times and incrementing 3 four times, that makes a[] = {4, 4, 8}

Input

arr = {2, 5, 9}, k = 2

Output

0

Approach used in the below program as follows

  • In main() function initialize int a[], size and k to store the array elements, size of array and the maximum updates possible respectively.

  • In max() function, first sort the array in ascending order and then declare two more arrays int p[size + 1] and m[size + 1] that will store prefix sum and maximum value respectively.

  • Loop from i = 0 till i<= size and initialize p[i] = 0 and m[i] = 0.

  • Outside the loop put m[0] = arr[0] and p[0] = arr[0].

  • Loop from i = 1 till i<size and put p[i] = p[i - 1] + arr[i] to calculate prefix sum and put m[i] = max(m[i - 1], arr[i]) to calculate maximum value up to that position.

  • After the loop, initialize int Lt = 1, Rt = size, result to store left value, right value and final answer respectively. After that initiate binary search.

  • Initiate while loop with condition (Lt < Rt). Inside the loop put int mid = (Lt + Rt) / 2. Check if (EleCal(p, m, mid - 1, k, size)). If so, then put result = mid and Lt = mid +1.

  • Else simply put Rt = mid -1.

  • Outside the loop print result.

  • In function bool EleCal() initiate for loop with condition for (int i = 0, j = x; j <= size; j++, i++)

  • Inside the loop check if (x * m[j] - (p[j] - p[i]) <= k). If so, then return true. Outside the loop return false.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
bool EleCal(int p[], int m[], int x, int k, int size){
   for (int i = 0, j = x; j <= size; j++, i++){
      if (x * m[j] - (p[j] - p[i]) <= k)
         return true;
   }
   return false;
}
void Max(int arr[], int size, int k){
   // sorting array in ascending order
   sort(arr, arr + size);
   int p[size + 1];
   //array for maximum value
   int m[size + 1];
   // Initialize prefix array and maximum array
   for (int i = 0; i <= size; ++i){
      p[i] = 0;
      m[i] = 0;
   }
   m[0] = arr[0];
   p[0] = arr[0];
   for (int i = 1; i < size; i++){
      // Calculating prefix sum of the array
      p[i] = p[i - 1] + arr[i];
      // Calculating max value upto that position
      // in the array
      m[i] = max(m[i - 1], arr[i]);
   }
   // Binary seach
   int Lt = 1, Rt = size, result;
   while (Lt < Rt){
      int mid = (Lt + Rt) / 2;
      if (EleCal(p, m, mid - 1, k, size)){
         result = mid;
         Lt = mid + 1;
      }
      else
         Rt = mid - 1;
   }
   //answer
   cout<<result;
}
// main function
int main(){
   int a[] = { 1, 3, 8 };
   int size = sizeof(a) / sizeof(a[0]);
   int k = 4;
   Max(a, size, k);
   return 0;
}

Output

2

Updated on: 04-Aug-2020

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements