703417. Equalize the Towers

Medium

Here is a detailed Markdown blog post for the "Equalize the Towers" problem:

Equalize the Towers

Summary

The goal of this problem is to equalize the heights of two towers. You are given an array of integers representing the heights of the towers at each minute, and you need to find the minimum number of operations required to make both towers have the same height at any given moment.

The key concepts involved in this problem include arrays and binary search.

Detailed Explanation

To solve this problem, we will use a two-pointer approach. We first sort the array of heights in ascending order. Then, we initialize two pointers, one at the beginning of the array and one at the end of the array. We then enter a loop where we keep moving the pointer that points to the shorter tower towards the other tower until both towers have the same height.

Here is a step-by-step breakdown of the solution:

  1. Sort the array of heights in ascending order.
  2. Initialize two pointers, left and right, at the beginning and end of the array, respectively.
  3. While left is less than or equal to right:
    • If the height at index left is less than the height at index right, move left one step to the right.
    • Otherwise, move right one step to the left.
  4. Once both towers have the same height, return the length of the array minus 1 as the minimum number of operations required.

The time complexity of this solution is O(n log n) due to the sorting step, where n is the size of the input array. The space complexity is O(1), as we only use a constant amount of space to store the pointers and the temporary variables.

Optimized Solutions

Java

java
public int equalizeTowers(int[] heights) {
    Arrays.sort(heights);
    int left = 0, right = heights.length - 1;
    while (left <= right) {
        if (heights[left] < heights[right]) {
            left++;
        } else {
            right--;
        }
    }
    return heights.length - 1;
}

Python

python
def equalize_towers(heights):
    heights.sort()
    left, right = 0, len(heights) - 1
    while left <= right:
        if heights[left] < heights[right]:
            left += 1
        else:
            right -= 1
    return len(heights) - 1

C++

cpp
int equalizeTowers(int* heights, int n) {
    std::sort(heights, heights + n);
    int left = 0, right = n - 1;
    while (left <= right) {
        if (heights[left] < heights[right]) {
            left++;
        } else {
            right--;
        }
    }
    return n - 1;
}

JavaScript

javascript
function equalizeTowers(heights) {
    heights.sort((a, b) => a - b);
    let left = 0, right = heights.length - 1;
    while (left <= right) {
        if (heights[left] < heights[right]) {
            left++;
        } else {
            right--;
        }
    }
    return heights.length - 1;
}
View on GeeksforGeeks

Not available

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.