
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Maximum array sum that can be obtained after exactly k changes in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Maximum subset with bitwise OR equal to k in C++
- C++ code to count maximum groups can be made
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- C++ program to count maximum possible division can be made in a graph with given condition
- Set the maximum height that a box can be with CSS
- Set the maximum width that a box can be with CSS
- Maximum sum subsequence with at-least k distant elements in C++
- Maximum litres of water that can be bought with N Rupees in C++
- Maximum number of people that can be killed with strength P in C++
- C++ Program to find out the maximum amount of money that can be made from selling cars
- Maximise the number of toys that can be purchased with amount K in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
