The key idea is increasing n - 1 elements by 1 has the same effect of decreasing 1 element by 1
So we first find the minimum value of the array and count the steps of decreasing every element to the minimum.
public class Solution {
public int minMoves(int[] nums) {
int min = Integer.MAX_VALUE;
for(int x : nums) {
min = Math.min(min, x);
}
int count = 0;
for(int x : nums) {
count = count + (x - min);
}
return count;
}
}
The key idea is increasing n - 1 elements by 1 has the same effect of decreasing 1 element by 1 So we first find the minimum value of the array and count the steps of decreasing every element to the minimum.
public class Solution { public int minMoves(int[] nums) { int min = Integer.MAX_VALUE; for(int x : nums) { min = Math.min(min, x); } int count = 0; for(int x : nums) { count = count + (x - min); } return count; } }