Skip to main content

Tower Heights

Challenge on lesson: Checkpoint — Sorting

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** A skyline has n towers with heights h_1..h_n. In one second you may pick any tower and change its height by 1. Find the **minimum total seconds** to make all towers equal, and print the common height achieving it. If several heights achieve the same minimum, print the smallest one. **Input:** Line 1: n (1 <= n <= 10^5). Line 2: n heights (0 <= h_i <= 10^9). **Output:** Line 1: minimum total change. Line 2: the optimal height (smallest if several tie). **Example:** 3 / 1 2 4 -> the median 2 gives 1+0+2 = 3 seconds; height 3 also gives 3, so print the smaller: 2.

Back to lesson: Checkpoint — Sorting