Consecutive Differences

EasyData EngineeringArray

Description

Given an array, return the differences between each element and the one before it. The output has one fewer element than the input; a single-element input returns an empty array.

Examples

Input:[1,3,6,10]
Output:[2,3,4]
Explanation:

Subtracting each element from its predecessor produces the step-by-step changes through the series.

Input:[5,5,5]
Output:[0,0]
Explanation:

Subtracting each element from its predecessor produces the step-by-step changes through the series.

Input:[10,7,1]
Output:[-3,-6]
Explanation:

Subtracting each element from its predecessor produces the step-by-step changes through the series.

Constraints

  • 1 ≤ length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.