12. Minimize the Heights II
β GFG solution to the Minimize the Heights II problem: find minimum difference between tallest and shortest towers after mandatory height modifications using greedy approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given an array arr[]
denoting heights of n
towers and a positive integer k
, you must perform exactly one of the following operations on each tower:
Increase the height of the tower by
k
Decrease the height of the tower by
k
Your task is to find the minimum possible difference between the height of the shortest and tallest towers after modification.
Important: After operations, the resultant array should not contain any negative integers.
π Examples
Example 1
Input: k = 2, arr[] = [1, 5, 8, 10]
Output: 5
Explanation: The array can be modified as [1+k, 5-k, 8-k, 10-k] = [3, 3, 6, 8].
The difference between the largest and smallest is 8-3 = 5.
Example 2
Input: k = 3, arr[] = [3, 9, 12, 16, 20]
Output: 11
Explanation: The array can be modified as [3+k, 9+k, 12-k, 16-k, 20-k] = [6, 12, 9, 13, 17].
The difference between the largest and smallest is 17-6 = 11.
π Constraints
$1 \le k \le 10^7$
$1 \le n \le 10^5$
$1 \le arr[i] \le 10^7$
β
My Approach
The optimal approach uses a Greedy Algorithm combined with Sorting to systematically evaluate all possible configurations:
Greedy Split-Point Strategy
Sort the Array:
Sort towers by height to enable greedy decision making.
This allows us to analyze potential split points systematically.
Initial Configuration:
Calculate the difference with all towers increased by
k
:arr[n-1] - arr[0]
.This serves as our baseline answer.
Evaluate Split Points:
For each position
i
from 1 to n-1, consider splitting operations:Towers
[0...i-1]
: Addk
to their heightsTowers
[i...n-1]
: Subtractk
from their heights
Skip configurations where
arr[i] - k < 0
(negative height constraint).
Calculate Optimal Range:
For each valid split:
Minimum height:
min(arr[0] + k, arr[i] - k)
Maximum height:
max(arr[n-1] - k, arr[i-1] + k)
Update answer with minimum difference found.
Mathematical Insight:
The optimal solution lies at one of the split points where we transition from adding
k
to subtractingk
.By testing all valid splits, we guarantee the optimal result.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the array. The sorting operation dominates with O(n log n), followed by a linear scan of O(n).
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables and perform in-place sorting.
π§βπ» Code (C++)
class Solution {
public:
int getMinDiff(vector<int>& a, int k) {
int n = a.size();
sort(a.begin(), a.end());
int ans = a[n-1] - a[0];
for (int i = 1; i < n; i++) {
if (a[i] >= k) {
int mn = min(a[0] + k, a[i] - k);
int mx = max(a[n-1] - k, a[i-1] + k);
ans = min(ans, mx - mn);
}
}
return ans;
}
};
β Code (Java)
class Solution {
public int getMinDiff(int[] a, int k) {
Arrays.sort(a);
int n = a.length, ans = a[n-1] - a[0];
for (int i = 1; i < n; i++) {
if (a[i] >= k) {
int mn = Math.min(a[0] + k, a[i] - k);
int mx = Math.max(a[n-1] - k, a[i-1] + k);
ans = Math.min(ans, mx - mn);
}
}
return ans;
}
}
π Code (Python)
class Solution:
def getMinDiff(self, a, k):
a.sort()
n = len(a)
ans = a[n-1] - a[0]
for i in range(1, n):
if a[i] >= k:
mn = min(a[0] + k, a[i] - k)
mx = max(a[n-1] - k, a[i-1] + k)
ans = min(ans, mx - mn)
return ans
π§ 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