Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created May 19, 2021 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/6acd7548c8fddca5fbfb7cfbb5bf4458 to your computer and use it in GitHub Desktop.
Save vrat28/6acd7548c8fddca5fbfb7cfbb5bf4458 to your computer and use it in GitHub Desktop.
Minimum Moves to Equal array
class Solution {
func minMoves2(_ nums: [Int]) -> Int {
let nums = nums.sorted()
let median = nums[nums.count/2]
var moves = 0
for num in nums{
moves += abs(median - num)
}
return moves
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment