aboutsummaryrefslogtreecommitdiff
path: root/max_diff_between_adj_elem.c
blob: a96f02c153337f9bc3c4085c1a0ab915e2e7a438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 3423. Maximum Difference Between Adjacent Elements in a Circular Array
#include "leetcode.h"

/*
 * given a circular array 'nums', find the maximum absolute difference between
 * adjacent elements. note, in a circular array, the first and last elements are
 * adjacent.
 */

int maxAdjacentDistance(int *nums, int numsSize) {
  int ans = abs(nums[0] - nums[numsSize - 1]);
  for (int i = 1; i < numsSize; i++) {
    int curr = abs(nums[i - 1] - nums[i]);
    ans = fmax(ans, curr);
  }
  return ans;
}

int main() {
  int n1[] = {1, 2, 4}, n2[] = {-5, -10, -5};
  printf("%d\n", maxAdjacentDistance(n1, ARRAY_SIZE(n2))); // expect: 3
  printf("%d\n", maxAdjacentDistance(n1, ARRAY_SIZE(n2))); // expect: 5
}