Maximize median after doing K addition operation on the Array
Last Updated :
01 Apr, 2021
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:
- Sort the array in increasing order.
- 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.
- Perform the operation in the second half and start performing the operations from the n/2th element to the end.
- If N is even then start doing the operation from the n/2 element to the end.
- Using Binary Search we will check for any number is possible as a median or not after doing K operation.
- 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++
#include <bits/stdc++.h>
using namespace std;
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) {
add += (mid - arr[i]);
if (add > K)
return false ;
}
}
if (add <= K)
return true ;
else
return false ;
}
int findMaxMedian( int arr[], int N,
int K)
{
int low = 1;
int mx = 0;
for ( int i = 0; i < N; ++i) {
mx = max(mx, arr[i]);
}
long long int high = K + mx;
while (low <= high) {
int mid = (high + low) / 2;
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 low - 1;
}
int main()
{
int arr[] = { 1, 3, 6 };
int K = 10;
int N = sizeof (arr) / sizeof (arr[0]);
sort(arr, arr + N);
cout << findMaxMedian(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
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 )
{
add += (mid - arr[i]);
if (add > K)
return false ;
}
}
if (add <= K)
return true ;
else
return false ;
}
static int findMaxMedian( int arr[], int N,
int K)
{
int low = 1 ;
int mx = 0 ;
for ( int i = 0 ; i < N; ++i)
{
mx = Math.max(mx, arr[i]);
}
int high = K + mx;
while (low <= high)
{
int mid = (high + low) / 2 ;
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 low - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 6 };
int K = 10 ;
int N = arr.length;
Arrays.sort(arr);
System.out.println(findMaxMedian(arr, N, K));
}
}
|
Python3
def possible(arr, N, mid, K):
add = 0
for i in range (N / / 2 - (N + 1 ) % 2 , N):
if (mid - arr[i] > 0 ):
add + = (mid - arr[i])
if (add > K):
return False
if (add < = K):
return True
else :
return False
def findMaxMedian(arr, N,K):
low = 1
mx = 0
for i in range (N):
mx = max (mx, arr[i])
high = K + mx
while (low < = high):
mid = (high + low) / / 2
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 low - 1
if __name__ = = '__main__' :
arr = [ 1 , 3 , 6 ]
K = 10
N = len (arr)
arr = sorted (arr)
print (findMaxMedian(arr, N, K))
|
C#
using System;
class GFG{
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)
{
add += (mid - arr[i]);
if (add > K)
return false ;
}
}
if (add <= K)
return true ;
else
return false ;
}
static int findMaxMedian( int []arr, int N,
int K)
{
int low = 1;
int mx = 0;
for ( int i = 0; i < N; ++i)
{
mx = Math.Max(mx, arr[i]);
}
int high = K + mx;
while (low <= high)
{
int mid = (high + low) / 2;
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 low - 1;
}
public static void Main( string [] args)
{
int []arr = { 1, 3, 6 };
int K = 10;
int N = arr.Length;
Array.Sort(arr);
Console.Write(findMaxMedian(arr, N, K));
}
}
|
Javascript
<script>
function possible(arr, N, mid, K)
{
let add = 0;
for (let i = parseInt(N / 2, 10) - (N + 1) % 2; i < N; ++i) {
if (mid - arr[i] > 0) {
add += (mid - arr[i]);
if (add > K)
return false ;
}
}
if (add <= K)
return true ;
else
return false ;
}
function findMaxMedian(arr, N, K)
{
let low = 1;
let mx = 0;
for (let i = 0; i < N; ++i) {
mx = Math.max(mx, arr[i]);
}
let high = K + mx;
while (low <= high) {
let mid = parseInt((high + low) / 2, 10);
if (possible(arr, N, mid, K)) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (N % 2 == 0) {
if (low - 1 < arr[parseInt(N / 2)]) {
return parseInt((arr[parseInt(N / 2)] + low - 1) / 2, 10);
}
}
return low - 1;
}
let arr = [ 1, 3, 6 ];
let K = 10;
let N = arr.length;
arr.sort();
document.write(findMaxMedian(arr, N, K));
</script>
|
Time Complexity: O(N*log(K + M)), where M is the maximum element of the given array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...