Array Maximal Adjacent Difference
Description
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
arrayMaximalAdjacentDifference(inputArray) = 3
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] array.integer inputArray
Guaranteed constraints:
3 ≤ inputArray.length ≤ 10
,
-15 ≤ inputArray[i] ≤ 15
.
- [output] boolean
The maximal absolute difference.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
function arrayMaximalAdjacentDifference(inputArray) {
var max = Math.abs(inputArray[1] - inputArray[0]);
for(var i=2; i < inputArray.length; i++) {
if(Math.abs(inputArray[i] - inputArray[i-1]) > max) {
max = Math.abs(inputArray[i] - inputArray[i-1]);
}
}
return max
}