commit 4ada2a99189b5fb1f752992116c77179ef92fa1f
parent 1276be41988ca8331a45ce13b2d54c32f67a23ce
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 12 Jun 2025 01:13:00 -0600
3423. maximum difference between adjacent elements in a circular queue
Diffstat:
2 files changed, 50 insertions(+), 0 deletions(-)
diff --git a/max_diff_between_adj_elem.c b/max_diff_between_adj_elem.c
@@ -0,0 +1,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
+}
diff --git a/max_diff_between_adj_elem.py b/max_diff_between_adj_elem.py
@@ -0,0 +1,27 @@
+# 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.
+"""
+
+
+class Solution(object):
+ def maxAdjacentDistance(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ n = len(nums)
+ ans = abs(nums[0] - nums[n - 1])
+ for i in range(1, n):
+ curr = abs(nums[i - 1] - nums[i])
+ ans = max(ans, curr)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxAdjacentDistance(nums=[1, 2, 4]))
+ print(obj.maxAdjacentDistance(nums=[-5, -10, -5]))