We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi, no code, but the solution idea is very simple:
To give to all except one means just to take from this one. So, the idea is to find the sum of "take" operations needed to make each item equal to the minimum.
However, it could be that the minimum is not reachable or reachable in a non-optimal way, so we should also consider taking from the minimum itself.
But it is senseless to take from the minimum the maximum piece or even more (it is obviously non-optimal — we will need to do the same with the rest as well). Therefore, we should consider possible alignments to min, min - 1, min - 2, min - 3, and min - 4.
Finally, just find the minimum of the sum of operations for each corresponding target.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal
You are viewing a single comment's thread. Return to all comments →
Hi, no code, but the solution idea is very simple:
To give to all except one means just to take from this one. So, the idea is to find the sum of "take" operations needed to make each item equal to the minimum.
However, it could be that the minimum is not reachable or reachable in a non-optimal way, so we should also consider taking from the minimum itself.
But it is senseless to take from the minimum the maximum piece or even more (it is obviously non-optimal — we will need to do the same with the rest as well). Therefore, we should consider possible alignments to
min
,min - 1
,min - 2
,min - 3
, andmin - 4
.Finally, just find the minimum of the sum of operations for each corresponding target.