#K75942. Maximum Adjacent Difference
Maximum Adjacent Difference
Maximum Adjacent Difference
You are given a list of integers representing ratings of products. Your task is to compute the maximum absolute difference between the ratings of any two adjacent products in the list.
Formally, suppose the list is represented as \(a_1, a_2, \dots, a_n\). You need to find:
[ \max_{2 \leq i \leq n} |a_i - a_{i-1}| ]
If the list contains fewer than two elements, the maximum difference is considered to be 0.
Examples:
- For the list
[5, 2, 9, 4, 7]
, the maximum difference is \(7\) (achieved between 2 and 9). - For the list
[1, 3, 3, 7]
, the maximum difference is \(4\) (achieved between 3 and 7). - For the list
[10, 15, 20]
, the maximum difference is \(5\) (the difference between consecutive elements is 5).
inputFormat
The first line of input contains an integer \(n\) representing the number of products. The second line contains \(n\) integers separated by spaces, representing the ratings of the products.
If \(n < 2\), you should output 0.
outputFormat
Output a single integer which is the maximum absolute difference between any two adjacent ratings in the list.
## sample5
5 2 9 4 7
7