




Sponsored
Sponsored
The problem can be simplified by observing that incrementing n-1 elements is equivalent to decrementing 1 element. Thus, the number of moves required to make all elements in the array equal is equal to the total number of moves required to make all elements equal to the minimum element present in the array.
To achieve this, calculate the difference between each element and the minimum element, summing these differences yields the required number of moves.
Time Complexity: O(n), Space Complexity: O(1)
1def min_moves(nums):
2    min_element = min(nums)
3    return sum(num - min_element for num in nums)
4
5nums = [1, 2, 3]
6print(min_moves(nums))The Python solution leverages Python's built-in functions to determine the minimum and accumulate the differences succinctly.