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 be
solution(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;
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *