# Solution For Minimum Moves To Equal Array Elements

Problem Statement:

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 or decrement an element of the array by 1.

Example 1:

Input: nums = [1,2,3] Output: 2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]

Example 2:

Input: nums = [1,2,4] Output: 3

Solution:

To make all the elements of the array equal, what we can do is find the minimum and maximum element of the array and then take the difference between them. That difference will be the number of moves required to make all the elements equal.

For example, if we have an array of [1, 2, 4], the minimum element is 1 and the maximum element is 4. The difference between them is 4-1=3, so we need 3 moves to make all the elements of the array equal.

There is one more thing we need to consider, which is the case where the minimum element is negative. In this case, we need to add the absolute value of the minimum element to all the elements in the array. This will make the minimum element zero and all the other elements will be adjusted accordingly. For example, if we have an array of [-1, 2, 4], the minimum element is -1. We add 1 to all the elements and the array becomes [0, 3, 5]. Now the minimum element is zero and the maximum element is 5, so we need 5 moves to make all the elements equal.

Hence, the solution to the problem can be implemented as follows:

Step 1: Initialize variables min_element, max_element, and sum_elements with the first element of the array nums.
Step 2: Loop through the remaining elements of nums and update min_element, max_element and sum_elements accordingly.
Step 3: If min_element is negative, add the absolute value of min_element to all elements of nums.
Step 4: Calculate the difference between max_element and min_element (now zero after addition in step 3) and return it as the answer.

## Step by Step Implementation For Minimum Moves To Equal Array Elements

```public int minMoves(int[] nums) {

// check for null and empty array
if (nums == null || nums.length == 0) {
return 0;
}

// find the minimum element in the array
int min = nums[0];
for (int i = 1; i < nums.length; i++) {
min = Math.min(min, nums[i]);
}

// calculate the number of moves needed to make all elements equal to the minimum element
int moves = 0;
for (int i = 0; i < nums.length; i++) {
moves += nums[i] - min;
}

return moves;
}```
```class Solution:
def minMoves(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# find minimum element in the array
min_num = min(nums)
# initialize result
result = 0
# iterate over the array
for i in range(len(nums)):
# for each element, add the difference between it and the min element to the result
result += (nums[i] - min_num)
# return the result
return result```
```var minMoves = function(nums) {
// find the minimum element in the array
let min = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}

// calculate the sum of moves needed to make all elements equal to the min element
let moves = 0;
for (let i = 0; i < nums.length; i++) {
moves += nums[i] - min;
}

return moves;
};```
```int minMoves(vector& nums) {
int min = nums[0];
for (int i = 1; i < nums.size(); i++) {
if (nums[i] < min) {
min = nums[i];
}
}
int res = 0;
for (int i = 0; i < nums.size(); i++) {
res += nums[i] - min;
}
return res;
}```
```public int MinMoves(int[] nums) {

// check for edge cases
if (nums == null || nums.Length <= 1) {
return 0;
}

// find the min element in the array
int min = nums[0];
for (int i = 1; i < nums.Length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}

// calculate and return the number of moves needed
int moves = 0;
for (int i = 0; i < nums.Length; i++) {
moves += nums[i] - min;
}
return moves;
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]