aboutsummaryrefslogtreecommitdiff
path: root/min_moves_equal_array.c
blob: 4a4b42ed2b8ca94ff2b4e24eafb416b6348dc0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 453. Minimum Moves to Equal Array Elements
#include "leetcode.h"

/*
 * 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 'n -
 * 1' elements of the array by 1
 */

int minMoves(int *nums, int numsSize) {
  int sum = 0, min = nums[0];
  for (int i = 0; i < numsSize; i++) {
    sum += nums[i];
    min = fmin(min, nums[i]);
  }
  return sum - numsSize * min;
}

int main() {
  int n1[] = {1, 2, 3}, n2[] = {1, 1, 1};
  printf("%d\n", minMoves(n1, ARRAY_SIZE(n1))); // expect: 3
  printf("%d\n", minMoves(n2, ARRAY_SIZE(n2))); // expect: 0
}