453. Minimum Moves to Equal Array Elements
1 min read
LeetCode Daily Problem: Minimum Moves to Equal Array Elements
On this page
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment n - 1 elements of the array by 1.
Constraints:
n == nums.length1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9- The answer is guaranteed to fit in a 32-bit integer.
Approach: Math
Incrementing n - 1 elements by 1 is equivalent to decrementing the remaining element by 1. Yes, it is that simple.
Code
python
class Solution:
def minMoves(self, nums: list[int]) -> int:
min_num = min(nums)
return sum((num-min_num for num in nums))Complexity
- Time: O(n) — one pass.
- Space: O(1).
SeriesDaily Problem →
TagsMath