Skip to content

Commit 1b560a3

Browse files
authored
Update Minimum Moves to Equal Array Elements II.java
1 parent 46a8666 commit 1b560a3

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
class Solution {
2-
public int minMoves2(int[] nums) {
3-
int i = 0;
4-
int j = nums.length-1;
5-
int c = 0;
6-
7-
Arrays.sort(nums);
8-
9-
while (i < j) {
10-
c += nums[j] - nums[i];
11-
i++;
12-
j--;
13-
}
14-
15-
return c;
2+
public int minMoves2(int[] nums) {
3+
Arrays.sort(nums);
4+
int sum = 0;
5+
int start = 0;
6+
int end = nums.length - 1;
7+
while (start < end) {
8+
sum += nums[end--] - nums[start++];
169
}
10+
return sum;
11+
}
1712
}

0 commit comments

Comments
 (0)