Array Maximal Adjacent Difference
Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.
Example
For inputArray = [2, 4, 1, 0]
, the output should be
array_maximal_adjacent_difference(inputArray) = 3
Solution
py
def array_maximal_adjacent_difference(inputArray):
md = 0
for i in range(len(inputArray) - 1):
md = max(md, abs(inputArray[i] - inputArray[i + 1]))
return md
print(array_maximal_adjacent_difference([9, 2, 0, 5]))
js
function arrayMaximalAdjacentDifference(inputArray) {
let max = 0;
for (let i = 0; i < inputArray.length - 1; i++) {
const diff = Math.abs(inputArray[i] - inputArray[i + 1]);
if (diff > max) {
max = diff;
}
}
return max;
}
console.log(arrayMaximalAdjacentDifference([9, 2, 0, 5]));