#include < vector >
using namespace std;
class GuessingNextElement
... {
public:
int guess(vector<int> A)
...{
if (A[1]-A[0]==A[2]-A[1])...{
return A[A.size()-1]+A[1]-A[0];
}
else
return A[A.size()-1]*(A[1]/A[0]);
}
} ;
Problem Statement | |||||||||||||
An integer arithmetic progression is a sequence defined by two positive integers, p and q, where p is the first element in the sequence, and all other elements are obtained by adding q to the previous element. For example, if p = 1 and q = 2, the sequence would be: 1, 3, 5, 7, ... An integer geometric progression is a sequence defined by two positive integers, p and q, where p is the first element in the sequence, and all other elements are obtained by multiplying the previous element by q. For example, if p = 3 and q = 2, the sequence would be: 3, 6, 12, ... You are given a vector <int> A, which contains either an integer arithmetic or geometric progression. Determine which one it is and return the next element in the sequence. It is guaranteed that A will uniquely represent either an arithmetic or geometric progression and that result will fit in a 32-bit signed integer. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | A will contain between 3 and 50 elements, inclusive. | ||||||||||||
- | Each element of A will be between 1 and 10^6, inclusive. | ||||||||||||
- | A will be sorted in ascending order. | ||||||||||||
- | A will uniquely represent either an arithmetic or geometric progression. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.