HELP required with greedy Problem "Minimize the height"

Revision en1, by amanjainnn, 2020-11-23 07:10:55

The problem

Given heights of n towers and a value k. We need to either increase or decrease the height of every tower by k (only once) where k > 0. The task is to minimize the difference between the heights of the longest and the shortest tower after modifications and output this difference.

Constraints

1 ≤ K ≤ 10^4 1 ≤ N ≤ 10^5 1 ≤ Arr[i] ≤ 10^5

Example: K=2, N=4 arr[]= [1,5,8,10]

Output: 5

P.S I already have the solution of GeeksforGeeks but that editorial solution is incorrect.

Can someone share an optimal and correct solution for this problem?

Tags greedy, minimization, #optimization

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English amanjainnn 2020-11-23 07:10:55 657 Initial revision (published)