CodeSignal Arcade – arrayMaximalAdjacentDifference
Problem
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 besolution(inputArray) = 3
.
Solution
Idea
Nothing to say about this problem. It’s too easy.
Code
function solution(inputArray) {
let maxDiff = 0;
for (let i = 1; i < inputArray.length; i++) {
const diff = Math.abs(inputArray[i] - inputArray[i-1]);
if (diff > maxDiff) {
maxDiff = diff;
}
}
return maxDiff;
}