Skip to content

Commit 682ccc3

Browse files
committed
Add solution
1 parent 3b31f6f commit 682ccc3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 3423. Maximum Difference Between Adjacent Elements in a Circular Array
2+
// Given a circular array nums, find the maximum absolute difference between adjacent elements.
3+
// Note: In a circular array, the first and last elements are adjacent.
4+
5+
6+
// Solution:
7+
8+
// Compare each adjacent pair of numbers and record the maximum absolute difference.
9+
// For the circular property, compare the first and last numbers.
10+
11+
// Time Complexity: O(n) 0ms
12+
// Space Complexity: O(1) 55MB
13+
function maxAdjacentDistance(nums) {
14+
const n = nums.length;
15+
let maxDiff = Math.abs(nums[0] - nums[n - 1]);
16+
for (let i = 1; i < n; i++) {
17+
maxDiff = Math.max(maxDiff, Math.abs(nums[i] - nums[i - 1]));
18+
}
19+
return maxDiff;
20+
};
21+
22+
// Two test cases
23+
console.log(maxAdjacentDistance([1,2,4])) // 3
24+
console.log(maxAdjacentDistance([-5,-10,-5])) // 5

0 commit comments

Comments
 (0)