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.
Input/Output
[execution time limit] 3 seconds (java)
[memory limit] 1 GB
[input] array.integer inputArray
Guaranteed constraints:
3 ≤ inputArray.length ≤ 10,
15 ≤ inputArray[i] ≤ 15.[output] integer
The maximal absolute difference.
int solution(int[] inputArray) {
// 인접한 두 수의 차이가 가장 큰 차를 찾는 문제
int max = Integer.MIN_VALUE;
for (int i = 0; i < inputArray.length - 1; i ++) {
// 현재 수와 다음 수의 차이를 구한다. (절대값)
int diff = Math.abs(inputArray[i] - inputArray[i + 1]);
// 비교 후, 최대값을 갱신한다.
if (max < diff) max = diff;
}
return max;
}