VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimize-cost-operation-equalize-tower-heights/

⇱ Minimize cost of operation to equalize tower heights - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimize cost of operation to equalize tower heights

Last Updated : 1 Aug, 2025

Given the heights of n towers in the array heights[], the task is to make all towers the same height by either adding or removing blocks.
Each addition or removal operation has a different cost for each tower, specified in the array cost[].
The objective is to minimize the total cost required to equalize the heights of all towers.

Examples:

Input: heights[] = [1, 2, 3], cost[] = [10, 100, 1000]
Output: 120
Explanation: We try all possible target heights and calculate the cost to make all towers equal to that height. The minimum cost comes when we raise tower 1 by 2 and tower 2 by 1, totaling 120, which is the lowest among all options.

Input: heights[] = [7, 1, 5], cost[] = [1, 1, 1]
Output: 6
Explanation: 1 * abs(7-5) + 1 * abs(1-5) + 1 * abs(5-5) = 6
(Taking 5 as final height)

[Naive Approach] Check All Possibilities – O(n^2) Time and O(1) Space

The idea is to iterate through each tower, and find the total cost of making heights of all towers to current tower. If total cost is less than current result, update the result.


Output
120

[Expected Approach] Binary Search – O(n × log2(max(heights[i]) - min(heights[i])) Time and O(1) Space

At minimum or maximum heights, the cost increases due to many changes in heights. As we move toward a balanced height, the cost drops, reaches a minimum, then rises again — forming a V-shape. We use binary search by checking mid - 1, mid, and mid + 1 to move toward the side with lower cost and find the minimum cost efficiently.

Step by step approach:

  • Find the minimum and maximum height in the heights[] array and use them as the initial range for binary search.
  • Apply binary search on this range. For each mid-point mid, calculate the total cost of making all towers of height: mid - 1, mid, mid + 1
  • Check these cases:
    => If findCost(mid) ≤ findCost(mid - 1) and findCost(mid) ≤ findCost(mid + 1),
    → mid gives the minimum cost, return it.
    => If findCost(mid) ≤ findCost(mid - 1) and findCost(mid + 1) < findCost(mid),
    → Move right: set low = mid + 1 (we are in the decreasing part of the cost curve).
    => If findCost(mid - 1) < findCost(mid),
    → Move left: set high = mid - 1 (we are in the increasing part of the cost curve).
  • Repeat until the minimum cost is found.

Output
120

[Alternate Approach] Ternary Search

At low or high heights, the cost increases due to excessive changes in heights. As we move toward an optimal height, the cost decreases, reaches a minimum, then increases again — forming a V-shape (unimodal pattern). This allows us to use Ternary search to efficiently find the minimum cost.

Step by step approach:

  • Each tower must be adjusted to a target height with cost: abs(height - target) * cost.
  • The target height will always lie between the minimum and maximum of the given tower heights.
  • The total cost function is unimodal, it decreases, reaches a minimum, then increases.
  • Therefore, ternary search is used to efficiently find the minimum cost.
  • In each step, two midpoints are chosen, and total cost is calculated at both.
  • If both costs are equal, shrink both ends of the range.
  • If the left cost is smaller, discard the right part.
  • If the right cost is smaller, discard the left part.
  • After the search ends, the minimum cost found is the final answer.

Output
120

Time Complexity: O(n × log3(max(heights[i]) - min(heights[i]))), ternary search is applied over the range from the minimum to the maximum tower height. In each step, calculating the total cost takes O(n) time, and the number of steps is logarithmic in the height difference.
Auxiliary Space: O(1), the algorithm uses only a constant amount of extra variables for computation, regardless of the number of towers.

Comment
Article Tags:
Article Tags: