Created
May 19, 2021 09:44
-
-
Save vrat28/6acd7548c8fddca5fbfb7cfbb5bf4458 to your computer and use it in GitHub Desktop.
Minimum Moves to Equal array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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