aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-06-12 01:13:00 -0600
committerJack Sangdahl <[email protected]>2025-06-12 01:13:00 -0600
commit4ada2a99189b5fb1f752992116c77179ef92fa1f (patch)
tree16a835929b6ee099b85ed12bb8a29b3515b9af11
parent1276be41988ca8331a45ce13b2d54c32f67a23ce (diff)
downloadleetcode-4ada2a99189b5fb1f752992116c77179ef92fa1f.tar.gz
leetcode-4ada2a99189b5fb1f752992116c77179ef92fa1f.zip
3423. maximum difference between adjacent elements in a circular queue
-rw-r--r--max_diff_between_adj_elem.c23
-rw-r--r--max_diff_between_adj_elem.py27
2 files changed, 50 insertions, 0 deletions
diff --git a/max_diff_between_adj_elem.c b/max_diff_between_adj_elem.c
new file mode 100644
index 0000000..a96f02c
--- /dev/null
+++ 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
new file mode 100644
index 0000000..4334537
--- /dev/null
+++ 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]))