Problem Statement

Given a sequence of integers, s,s,..,s[n] we can define its difference sequence as the sequence s-s, s-s, ..., 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            -30
Given 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

 Class: DifDif Method: predict Parameters: int[] Returns: int Method signature: int predict(int[] seq) (be sure your method is public)

Constraints

-seq will contain between 1 and 10 elements, inclusive.
-Each element of seq will be between -1000 and 1000, inclusive.

Examples

0)

 {5,-4, 12, 23}
Returns: -1
 This is the example given above.
1)

 {100}
Returns: 100
 The first difference sequence of 100,100 is a sequence consisting of one 0.
2)

 {1,4,9,16,25,36}
Returns: 49
3)

 {-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000}
Returns: 1023000

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7296

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10899&pm=7296

dgoodman

Testers:

PabloGilberto , Olexiy , ivan_metelsky , andrewzta

Math