To know how many sequences we can build for position i it is enough to know for how many sequences(with length>2) it can be the tail.

For example: {1,2,4,6,8,10,11 }

The number 8 at position 4 (0-based) can be tail for 2 sequences:

- [2,4,6,8];
- [4,6,8];

The number 10 can be tail for 3 sequence:

- [2,4,6,8,10]
- [4,6,8,10]
- [6,8,10]

The sum of values of all tails will give us the number of possible sequences.

```
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
HashMap<Integer, Integer> differences = new HashMap<>();
int d[] = new int[A.length];
int sum = 0;
for (int i=2; i<A.length; i++) {
if (A[i]-A[i-1] == A[i-1]-A[i-2]) {
d[i] = d[i-1]+1;
}
sum+=d[i];
}
return sum;
}
}
```

Optimization for memory:

However, for calulating the length of sequence with equal differences at position i it is enough to know the answer only for position i-1. It is enough to store the value for i-1;

```
public class Solution {
public int numberOfArithmeticSlices(int[] A) {
int sum = 0;
int cur = 0;
for (int i=2; i<A.length; i++) {
if ( A[i]-A[i-1] == A[i-1]-A[i-2] ) {
cur++;
sum+=cur;
} else {
cur = 0;
}
}
return sum;
}
}
```