spiralgo / algorithms

We voluntarily solve algorithm problems on this repo.
24 stars 7 forks source link

453. Minimum Moves to Equal Array Elements #361

Closed ErdemT09 closed 3 years ago

ErdemT09 commented 3 years ago

Problem: https://leetcode.com/problems/minimum-moves-to-equal-array-elements/

Here, incrementing all numbers except one is equivalent to decrementing that one number, as we only care about the fact that the numbers are equal. Thus, the solution is equivalent to decrementing every number to the minimum in the array. The amount of operations done to the each element would result in the general formula sum - length*minimum.

Array problems as such are good build-ups for this larger problem: #212

ErdemT09 commented 3 years ago

Problem for the follow-up: https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii How can we find the median of an unsorted array in O(n) time?