Connection Status:
Competition Arena > DifDif
TCCC07 Sponsor 1 · 2007-07-30 · by dgoodman · Math
Class Name: DifDif
Return Type: int
Method Name: predict
Arg Types: (vector<int>)
Problem Statement

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            -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.

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
4)
{-512,-512}
Returns: -512

Submissions are judged against all 37 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class DifDif with a public method int predict(vector<int> seq) · 37 test cases · 2 s / 256 MB per case

Submitting as anonymous