I’d split the problem to several steps:
- Find the value index. If given an index you can skip this step.
- Compute the subtraction with the value or values below it.
- Compute the subtraction with the value or values above it.
Note that you should only base your run on indices, not values.
solved compare and subtract current value from next 3 value