25. Maximize Median After K Addition Operations
β 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. π
The problem can be found at the following link: π Question Link
π§© Problem Description
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 = 3
Output: 5
Explanation: We can add +2 to the second element and +1 to the third element to get the array [1, 5, 5, 5].
After sorting, the array remains [1, 5, 5, 5]. Since the length is even, the median is (5 + 5) / 2 = 5.Example 2
Input: arr[] = [1, 3, 6, 4, 2], k = 10
Output: 7
Explanation: After applying operations optimally, we can transform the array to [1, 3, 7, 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.π 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
koperations.
Validation Logic:
Odd length (n): Elements from position
n/2onwards must be β₯ target median.Even length (n): Both elements at positions
n/2-1andn/2must 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++)
β Code (Java)
π Code (Python)
π§ 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
Last updated