[LeetCode] Maximum Difference Between Adjacent Elements in a Circular Array

3423. Maximum Difference Between Adjacent Elements in a Circular Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import "math"

func maxAdjacentDistance(nums []int) int {
res := 0
n := len(nums)
for i := 0; i < n; i++ {
res = max(res, int(math.Abs(float64(nums[i]-nums[(i+1)%n]))))
}
return res
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/01/19/PS/LeetCode/maximum-difference-between-adjacent-elements-in-a-circular-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.