Problem Statement | |||||||||||||
Given a sequence of integers, s[0],s[1],..,s[n] we can define its difference sequence
as the sequence s[1]-s[0], s[2]-s[1], ..., s[n]-s[n-1]. We can similarly generate its
second difference sequence as the difference sequence of its difference sequence,
and continue generating deeper difference sequences until we get one with
length 1.
Here is an example: seq: 5 -4 12 23 1stdifseq -9 16 11 2nddifseq 25 -5 3rddifseq -30Given a sequence of integers, one useful way to predict the next value in the sequence is by choosing the one that will make the bottom difference of the enlarged sequence be 0. In the example, we would predict -1 as the next value in the sequence -- this would extend the first difference sequence to end with -1 - 23 = -24, the second to end with -35, and the third to end with -30. This would make the single value in the fourth sequence be 0. Given int[] seq, return the predicted value. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | seq will contain between 1 and 10 elements, inclusive. | ||||||||||||
- | Each element of seq will be between -1000 and 1000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|