# Maximize median after doing K addition operation on the Array

Given an array arr[] of N elements and an integer K, the task is to perform at most K operation on the array. In the one operation increment any element by one of the array. Find maximize median after doing K such operation.

Example:

Input: arr[] = {1, 3, 4, 5}, K = 3
Output: 5
Explanation: Here we add two in the second element and one in the third element then we will get a maximum median. After k operation the array can become {1, 5, 5, 5}. So the maximum median we can make is ( 5 + 5 ) / 2 = 5, because here N is even.

Input: arr[] = {1, 3, 6, 4, 2}, K = 10
Output: 7

Approach:

1. Sort the array in increasing order.
2. Since the median is the middle element of the array doing the operation in the left half then it will be worthless because it will not increase the median.
3. Perform the operation in the second half and start performing the operations from the n/2th element to the end.
4. If N is even then start doing the operation from the n/2 element to the end.
5. Using Binary Search we will check for any number is possible as a median or not after doing K operation.
6. If the median is possible then we will check for the next number which is greater than the current median calculated. Otherwise, the last possible value of the median is the required result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to check operation can be` `// perform or not` `bool` `possible(``int` `arr[], ``int` `N,` `              ``int` `mid, ``int` `K)` `{`   `    ``int` `add = 0;`   `    ``for` `(``int` `i = N / 2 - (N + 1) % 2;` `         ``i < N; ++i) {`   `        ``if` `(mid - arr[i] > 0) {`   `            ``// Number of operation to` `            ``// perform s.t. mid is median` `            ``add += (mid - arr[i]);`   `            ``if` `(add > K)` `                ``return` `false``;` `        ``}` `    ``}`   `    ``// If mid is median of the array` `    ``if` `(add <= K)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}`   `// Function to find max median` `// of the array` `int` `findMaxMedian(``int` `arr[], ``int` `N,` `                  ``int` `K)` `{`   `    ``// Lowest possible median` `    ``int` `low = 1;` `    ``int` `mx = 0;`   `    ``for` `(``int` `i = 0; i < N; ++i) {` `        ``mx = max(mx, arr[i]);` `    ``}`   `    ``// Highest possible median` `    ``long` `long` `int` `high = K + mx;`   `    ``while` `(low <= high) {`   `        ``int` `mid = (high + low) / 2;`   `        ``// Checking for mid is possible` `        ``// for the median of array after` `        ``// doing at most k operation` `        ``if` `(possible(arr, N, mid, K)) {` `            ``low = mid + 1;` `        ``}`   `        ``else` `{` `            ``high = mid - 1;` `        ``}` `    ``}`   `    ``if` `(N % 2 == 0) {`   `        ``if` `(low - 1 < arr[N / 2]) {` `            ``return` `(arr[N / 2] + low - 1) / 2;` `        ``}` `    ``}`   `    ``// Return the max possible ans` `    ``return` `low - 1;` `}`   `// Driver Code` `int` `main()` `{` `    ``// Given array` `    ``int` `arr[] = { 1, 3, 6 };`   `    ``// Given number of operation` `    ``int` `K = 10;`   `    ``// Size of array` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);`   `    ``// Sort the array` `    ``sort(arr, arr + N);`   `    ``// Function call` `    ``cout << findMaxMedian(arr, N, K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach ` `import` `java.util.*;`   `class` `GFG{`   `// Function to check operation can be` `// perform or not` `static` `boolean` `possible(``int` `arr[], ``int` `N,` `                        ``int` `mid, ``int` `K)` `{` `    ``int` `add = ``0``;`   `    ``for``(``int` `i = N / ``2` `- (N + ``1``) % ``2``; ` `            ``i < N; ++i)` `    ``{` `        ``if` `(mid - arr[i] > ``0``)` `        ``{`   `            ``// Number of operation to` `            ``// perform s.t. mid is median` `            ``add += (mid - arr[i]);`   `            ``if` `(add > K)` `                ``return` `false``;` `        ``}` `    ``}`   `    ``// If mid is median of the array` `    ``if` `(add <= K)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}`   `// Function to find max median` `// of the array` `static` `int` `findMaxMedian(``int` `arr[], ``int` `N,` `                                    ``int` `K)` `{`   `    ``// Lowest possible median` `    ``int` `low = ``1``;` `    ``int` `mx = ``0``;`   `    ``for``(``int` `i = ``0``; i < N; ++i)` `    ``{` `        ``mx = Math.max(mx, arr[i]);` `    ``}`   `    ``// Highest possible median` `    ``int` `high = K + mx;`   `    ``while` `(low <= high) ` `    ``{` `        ``int` `mid = (high + low) / ``2``;`   `        ``// Checking for mid is possible` `        ``// for the median of array after` `        ``// doing at most k operation` `        ``if` `(possible(arr, N, mid, K))` `        ``{` `            ``low = mid + ``1``;` `        ``}`   `        ``else` `        ``{` `            ``high = mid - ``1``;` `        ``}` `    ``}` `    `  `    ``if` `(N % ``2` `== ``0``) ` `    ``{` `        ``if` `(low - ``1` `< arr[N / ``2``])` `        ``{` `            ``return` `(arr[N / ``2``] + low - ``1``) / ``2``;` `        ``}` `    ``}` `    `  `    ``// Return the max possible ans` `    ``return` `low - ``1``;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    `  `    ``// Given array` `    ``int` `arr[] = { ``1``, ``3``, ``6` `};` `    `  `    ``// Given number of operation` `    ``int` `K = ``10``;` `    `  `    ``// Size of array` `    ``int` `N = arr.length;` `    `  `    ``// Sort the array` `    ``Arrays.sort(arr);` `    `  `    ``// Function call` `    ``System.out.println(findMaxMedian(arr, N, K));` `}` `}`   `// This code is contributed by offbeat`

## Python3

 `# Python3 program for the above approach`   `# Function to check operation can be` `# perform or not` `def` `possible(arr, N, mid, K):`   `    ``add ``=` `0`   `    ``for` `i ``in` `range``(N ``/``/` `2` `-` `(N ``+` `1``) ``%` `2``, N):`   `        ``if` `(mid ``-` `arr[i] > ``0``):`   `            ``# Number of operation to` `            ``# perform s.t. mid is median` `            ``add ``+``=` `(mid ``-` `arr[i])`   `            ``if` `(add > K):` `                ``return` `False`   `    ``# If mid is median of the array` `    ``if` `(add <``=` `K):` `        ``return` `True` `    ``else``:` `        ``return` `False`   `# Function to find max median` `# of the array` `def` `findMaxMedian(arr, N,K):`   `    ``# Lowest possible median` `    ``low ``=` `1` `    ``mx ``=` `0`   `    ``for` `i ``in` `range``(N):` `        ``mx ``=` `max``(mx, arr[i])`   `    ``# Highest possible median` `    ``high ``=` `K ``+` `mx`   `    ``while` `(low <``=` `high):`   `        ``mid ``=` `(high ``+` `low) ``/``/` `2`   `        ``# Checking for mid is possible` `        ``# for the median of array after` `        ``# doing at most k operation` `        ``if` `(possible(arr, N, mid, K)):` `            ``low ``=` `mid ``+` `1` `        ``else` `:` `            ``high ``=` `mid ``-` `1`   `    ``if` `(N ``%` `2` `=``=` `0``):`   `        ``if` `(low ``-` `1` `< arr[N ``/``/` `2``]):` `            ``return` `(arr[N ``/``/` `2``] ``+` `low ``-` `1``) ``/``/` `2`   `    ``# Return the max possible ans` `    ``return` `low ``-` `1`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Given array` `    ``arr ``=` `[``1``, ``3``, ``6``]`   `    ``# Given number of operation` `    ``K ``=` `10`   `    ``# Size of array` `    ``N ``=` `len``(arr)`   `    ``# Sort the array` `    ``arr ``=` `sorted``(arr)`   `    ``# Function call` `    ``print``(findMaxMedian(arr, N, K))`   `# This code is contributed by Mohit Kumar`

## C#

 `// C# program for the above approach ` `using` `System; ` `class` `GFG{` ` `  `// Function to check operation can be` `// perform or not` `static` `bool` `possible(``int` `[]arr, ``int` `N,` `                       ``int` `mid, ``int` `K)` `{` `    ``int` `add = 0;` ` `  `    ``for``(``int` `i = N / 2 - (N + 1) % 2; ` `            ``i < N; ++i)` `    ``{` `        ``if` `(mid - arr[i] > 0)` `        ``{` ` `  `            ``// Number of operation to` `            ``// perform s.t. mid is median` `            ``add += (mid - arr[i]);` ` `  `            ``if` `(add > K)` `                ``return` `false``;` `        ``}` `    ``}` ` `  `    ``// If mid is median of the array` `    ``if` `(add <= K)` `        ``return` `true``;` `    ``else` `        ``return` `false``;` `}` ` `  `// Function to find max median` `// of the array` `static` `int` `findMaxMedian(``int` `[]arr, ``int` `N,` `                                    ``int` `K)` `{` ` `  `    ``// Lowest possible median` `    ``int` `low = 1;` `    ``int` `mx = 0;` ` `  `    ``for``(``int` `i = 0; i < N; ++i)` `    ``{` `        ``mx = Math.Max(mx, arr[i]);` `    ``}` ` `  `    ``// Highest possible median` `    ``int` `high = K + mx;` ` `  `    ``while` `(low <= high) ` `    ``{` `        ``int` `mid = (high + low) / 2;` ` `  `        ``// Checking for mid is possible` `        ``// for the median of array after` `        ``// doing at most k operation` `        ``if` `(possible(arr, N, mid, K))` `        ``{` `            ``low = mid + 1;` `        ``}` ` `  `        ``else` `        ``{` `            ``high = mid - 1;` `        ``}` `    ``}` `     `  `    ``if` `(N % 2 == 0) ` `    ``{` `        ``if` `(low - 1 < arr[N / 2])` `        ``{` `            ``return` `(arr[N / 2] + low - 1) / 2;` `        ``}` `    ``}` `     `  `    ``// Return the max possible ans` `    ``return` `low - 1;` `}` ` `  `// Driver code` `public` `static` `void` `Main(``string``[] args)` `{` `     `  `    ``// Given array` `    ``int` `[]arr = { 1, 3, 6 };` `     `  `    ``// Given number of operation` `    ``int` `K = 10;` `     `  `    ``// Size of array` `    ``int` `N = arr.Length;` `     `  `    ``// Sort the array` `    ``Array.Sort(arr);` `     `  `    ``// Function call` `    ``Console.Write(findMaxMedian(arr, N, K));` `}` `}` ` `  `// This code is contributed by rock_cool`

Output:

```9

```

Time Complexity: O(N*log(K + M)), where M is the maximum element of the given array.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.