Problem Statement
There was a sequence A that had at least three elements.
The sequence B was obtained by writing down the average of each three consecutive elements of A, from left to right.
For example, if A = {a, b, c, d, e}, then the sequence B would have the form {f, g, h} where f = the average of a,b,c, g = the average of b,c,d, and h = the average of c,d,e.
You are given the sequence B. All its elements are integers. You are also given the information that the first two elements of A were zeros.
Reconstruct A and return it.
Definition
- Class:
- AveragesOfThree
- Method:
- restore
- Parameters:
- int[]
- Returns:
- int[]
- Method signature:
- int[] restore(int[] B)
- (be sure your method is public)
Notes
- The sequence A is always unique.
- For the given constraints it is guaranteed that all elements of A are integers that fit into signed 32-bit integer variables.
Constraints
- B will have between 1 and 10 elements, inclusive.
- Each element of B will be between 0 and 1000, inclusive.
Examples
{3}Returns: {0, 0, 9 }The original sequence was {0, 0, x}, and as the average of 0, 0, and x was 3, it is clear that x must have been 9.{1, 3, 6, 5}Returns: {0, 0, 3, 6, 9, 0 }{3, 1, 4, 1, 5, 9, 2, 6}Returns: {0, 0, 9, -6, 9, 0, 6, 21, -21, 18 }As seen here, some elements of the original sequence may be negative integers.