05. Minimize the Heights II
The problem can be found at the following link: Question Link
My Approach
Simply greddy approach, I start by sorting the array. Then, for each element, I consider two possibilities:
Getting max after adding
k
to the current element and subtractingk
from the maximum element.Getting min after subtracting
k
from the current element and addingk
to the minimum element. I update the minimum difference (out
) by comparing it with the difference obtained from the above two possibilities.
Time and Auxiliary Space Complexity
Time Complexity:
O(nlog n)
for sortingarr
array.Auxiliary Space Complexity:
O(1)
, as no extra space is used.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated