Compute the total number of steps needed to visit trees placed along a road in order.
Roadside Trees
gfgRoadside Trees (Simplified Edition)
A road has several trees planted at different positions along a straight line. You start at the first tree and move to the next tree in the given order. To go from one tree to the next, you need to walk the absolute distance between their positions.
Your task is to calculate the total distance walked while visiting all trees in order.
This is the simplified version of the problem, where the tree positions are already given in the order you must visit them.
Input Format
- The first line contains an integer — the number of trees.
- The second line contains integers — the positions of the trees in the order they are visited.
Output Format
- Output one integer: the total distance traveled when moving from the first tree to the second, then to the third, and so on.
Constraints
- The answer fits in a 64-bit signed integer.
Example 1
Input
5 1 5 2 8 3
Output
14
Explanation
The distances are |5-1| = 4, |2-5| = 3, |8-2| = 6, and |3-8| = 5. The total is 4 + 3 + 6 + 5 = 18.
(Note: for this illustrative example, the output should match the sum of consecutive absolute differences.)
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.