This problem is solvable - this is O (NlogN) time and O (1) constant space, where N is the number of towers.
We must make the towers equal to m equal to the height so that we build the minimum floors.
Important Observations
1 . Since we are building the minimum floors, and not building floors on the m towers and reaching equal heights, we will try to make m - 1 towers of the same height as the target towers.
2 . The optimal solution is to find such towers m that have the minimum sum of the height difference. Let this solution be towers having the following floors in ascending order :
m1 , m2 , m3 , ....... m floor
The optimal solution has the following property:
The sum of the differences is minimal :
(m - m1) + (m - m2) + (m - m3) + .....(m - (m-1))
Because only then can we say that we need to build a minimum number of floors.
. Thus, we need to find many m floors in such a way that the difference between their heights and the floor with the maximum height (within these m towers) is minimal .
Let's say we have an arr array of length n, representing the number of floors on each tower. First we sort towers by height.
Then we start to find the sum of differences as follows. So, we are in index i in. Find the following amount:
arr[i + m - 1] - arr[i] + arr[i + m - 2] - arr[i] + arr[i + m - 3] - arr[i] + arr[i + m - 4] - arr[i] . . . arr[i + 1] - arr[i]
Lets name the sum s1 . Now, when we consider the following set of towers m , which means towers from indices i + 1 to i + m , we do not need a cycle to calculate the sum of the differences, it can simply be derived from the following :
(m - 1)*arr[i + m] - s1 - ((m-1)*arr[i])
Thus, using this simple mathematical technique, we need only one loop for this problem.
Pseudocode for the proposed algorithm:
Sort the array arr of n towers. Compute the sum of differences for first set of m towers. sum = 0 for(i = 1 to m - 1) { sum = sum + arr[i] - arr[0] } Now for the remaining set of m towers previous_sum = sum, sum = 0, j = 1 minimum_sum = previous_sum for(i = m to n - 1) { sum = (m-1)*arr[i] - previous_sum - (m - 1)*arr[j - 1] increment j if( sum < minimum_sum ) { minimum_sum = sum } previous_sum = sum } The answer is minimum_sum.