




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)
1using System;
2using System.Linq;
3
4public class Program {
5    public static int MinMoves(int[] nums) {
6        int min = nums.Min();
7        return nums.Sum(num => num - min);
8    }
9
10    public static void Main() {
11        int[] nums = {1, 2, 3};
12        Console.WriteLine(MinMoves(nums));
13    }
14}This C# solution efficiently uses LINQ to calculate the minimum and sum up the differences, providing a clean and concise implementation.