β GFG solution to the Maximize Median After K Addition Operations problem: find maximum possible median after performing at most k increment operations using binary search and greedy strategy. π
You are given an array arr[] consisting of positive integers and an integer k. You are allowed to perform at most k operations, where in each operation, you can increment any one element of the array by 1. Determine the maximum possible median of the array that can be achieved after performing at most k such operations.
Note: The median of an array is defined as the middle element when the array (after sorting) has an odd size, or the average of the two middle elements when the array (after sorting) has an even size.
π Examples
Example 1
Input: arr[] = [1,3,4,5], k =3Output: 5Explanation: We can add +2 to the second element and+1 to the third element to get the array [1,5,
Example 2
Input: arr[] = [1,3,6,4,2], k =10Output: 7Explanation: After applying operations optimally, we can transform the array to [1,3,7,
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i], k \le 10^9$
β My Approach
The optimal approach uses Binary Search on the answer combined with a Greedy Strategy to check if a target median is achievable:
Binary Search + Greedy Validation
Key Insight:
To maximize the median, we need to focus on elements that contribute to the median calculation.
For odd-length arrays: median is the middle element after sorting.
For even-length arrays: median is the average of two middle elements.
Strategy:
Sort the array first to identify median positions.
Use binary search on possible median values.
For each candidate median, greedily check if it's achievable with at most k operations.
Validation Logic:
Odd length (n): Elements from position n/2 onwards must be β₯ target median.
Even length (n): Both elements at positions n/2-1 and n/2 must contribute to achieve target median average.
Greedy Check:
Calculate minimum operations needed to achieve target median.
If operations β€ k, the target is achievable.
Binary Search Bounds:
Lower bound: Current median value
Upper bound: Current median + k (maximum possible increase)
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n + n log k), where n is the size of the array. The sorting takes O(n log n) and binary search with validation takes O(n log k) as we perform at most O(log k) iterations, each requiring O(n) time for validation.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables, excluding the space used for sorting which is typically O(log n) for quicksort.
π§βπ» Code (C++)
class Solution {
public:
bool canAchieve(vector<int>& arr, int target, int k) {
int n = arr.size();
long long ops = 0;
if (n % 2 == 1) {
for (int i = n/2; i < n; i++) {
if (arr[i] < target) {
ops += target - arr[i];
if (ops > k) return false;
}
}
} else {
long long medianSum = arr[n/2-1] + arr[n/2];
if (2LL * target > medianSum) {
ops += 2LL * target - medianSum;
if (ops > k) return false;
}
for (int i = n/2+1; i < n; i++) {
if (arr[i] < target) {
ops += target - arr[i];
if (ops > k) return false;
}
}
}
return true;
}
int maximizeMedian(vector<int>& arr, int k) {
sort(arr.begin(), arr.end());
int n = arr.size();
int l = (n % 2 == 1) ? arr[n/2] : (arr[n/2-1] + arr[n/2]) / 2;
int r = l + k;
int ans = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (canAchieve(arr, mid, k)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
};
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Lambda Function Optimization
π‘ Algorithm Steps:
Use lambda function for cleaner code organization
Handle odd/even cases with proper median calculation logic
Early termination for efficiency in operations counting
Optimized binary search bounds
π Complexity Analysis:
Time: β±οΈ O(n log n + n log k)
Auxiliary Space: πΎ O(1) - Lambda function uses captured variables
β Why This Approach?
Compact and modern C++ style
Lambda function keeps validation logic contained
Same time complexity with cleaner code structure
π π Comparison of Approaches
π Best Choice Recommendation
β Code (Java)
class Solution {
public int maximizeMedian(int[] arr, int k) {
Arrays.sort(arr);
int n = arr.length;
int left = (n % 2 == 1) ? arr[n/2] : (arr[n/2-1] + arr[n/2]) / 2;
int right = left + k;
int result = left;
while (left <= right) {
int mid = left + (right - left) / 2;
if (canAchieve(arr, mid, k)) {
result = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
private boolean canAchieve(int[] arr, int target, int k) {
int n = arr.length;
long ops = 0;
if (n % 2 == 1) {
for (int i = n/2; i < n; i++) {
if (arr[i] < target) {
ops += target - arr[i];
if (ops > k) return false;
}
}
} else {
long medianSum = (long)arr[n/2-1] + arr[n/2];
if (2L * target > medianSum) {
ops += 2L * target - medianSum;
if (ops > k) return false;
}
for (int i = n/2+1; i < n; i++) {
if (arr[i] < target) {
ops += target - arr[i];
if (ops > k) return false;
}
}
}
return true;
}
}
π Code (Python)
class Solution:
def maximizeMedian(self, arr, k):
arr.sort()
n = len(arr)
left = arr[n//2] if n % 2 == 1 else (arr[n//2-1] + arr[n//2]) // 2
right = left + k
result = left
while left <= right:
mid = left + (right - left) // 2
if self.canAchieve(arr, mid, k):
result = mid
left = mid + 1
else:
right = mid - 1
return result
def canAchieve(self, arr, target, k):
n = len(arr)
ops = 0
if n % 2 == 1:
for i in range(n//2, n):
if arr[i] < target:
ops += target - arr[i]
if ops > k:
return False
else:
median_sum = arr[n//2-1] + arr[n//2]
if 2 * target > median_sum:
ops += 2 * target - median_sum
if ops > k:
return False
for i in range(n//2+1, n):
if arr[i] < target:
ops += target - arr[i]
if ops > k:
return False
return True
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Visitor counter
5
,
5
].
After sorting, the array remains [1,5,5,5]. Since the length is even, the median is (5+5) /2=5.
7
,
7
] (one possible way).
Sorted array becomes [1,3,7,7,7]. Since the length is odd, the median is the middle element 7.
class Solution {
public:
int maximizeMedian(vector<int>& arr, int k) {
sort(arr.begin(), arr.end());
int n = arr.size();
auto canAchieve = [&](int target) -> bool {
long long ops = 0;
if (n % 2 == 1) {
for (int i = n/2; i < n; i++) {
ops += max(0, target - arr[i]);
if (ops > k) return false;
}
} else {
ops += max(0LL, 2LL * target - (long long)arr[n/2-1] - arr[n/2]);
if (ops > k) return false;
for (int i = n/2+1; i < n; i++) {
ops += max(0, target - arr[i]);
if (ops > k) return false;
}
}
return true;
};
int l = (n % 2 == 1) ? arr[n/2] : (arr[n/2-1] + arr[n/2]) / 2;
int r = l + k;
int ans = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (canAchieve(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
};