Making everyone's salary equal in minimum number of steps?

55 Views Asked by At

Suppose there are n number of workers in an organization. Each worker has his/her own wage. The salary of the ith worker equals to Wi (i = 1, 2, ..., N).
The chief decided to equalize all workers, that is, he wants to make salaries of all workers to be equal. But for this goal the chief can use only one operation: choose some worker and increase the salary by 1 unit of each worker, except the salary of the chosen worker.
The chief wants to minimize the total number of operations needed to equalize all workers. What is the minimum number of steps required?
I've got the solution here.
It says, the minimum steps = sum(W) − n * min(W).
Where sum(W) is the sum of all the wages of all the employees and min(W) is minimum wage among all the workers.
What I don't understand is how the number of operations done on ith worker is equal to Salary of ith worker - Minimum Salary? I'm unable to understand the explanation given in the link.

2

There are 2 best solutions below

0
On BEST ANSWER

What I don't understand is how the number of operations done on i'th worker is equal to Salary of i'th worker - Minimum Salary.

You have understood that all that matters is the relative differences in salaries, and hence the minimum number of moves is unchanged if you modify the operation to be to decrease only one person's salary. Now observe that the original minimum salary $m$ would never be increased, so you have to decrease every other worker's salary by at least the difference between them in order to reach equality. This shows that you need at least $W_i-m$ moves on worker $i$. Also each move only affects one worker, so in total you need at least $\sum_{i=1}^n (W_i-m)$ moves. You can also check that indeed you can achieve equal salaries using exactly $W_i-m$ moves on each worker $i$. This shows that the minimum number of moves is indeed $\sum_{i=1}^n (W_i-m)$.

0
On

First step is to understand why the initial operation is equal to the operation "decrease an i'th workers' salary by 1". In this task you dont care about absolute numbers, only about differences between salaries. In that sense both operations(initial and the one suggested) change the difference by 1(+1 or -1). For example, you have an array [3 4 5]. If you apply the operation to the last worker you obtain the array [4 5 5] in case of the initial operation and [3 4 4] in case of the suggested one. Differences between salaries are the same but the absolute values are different.

Then you find a person with a lowest salary($\min(W_i)$). For each worker you apply the "decrease by 1" operation to their salary. For i'th worker the number of operations needed is $W_i - \min(W_i)$. The sum is exactly the result you need.