PROBLEM STATEMENT
There have been countless sequences published in puzzle books where you are
given an incomplete sequence and must find some element. Two of the most
common types of sequences are arithmetic sequences and geometric sequences. An
arithmetic sequence is one in which every term but the first is the sum of the
previous term and some constant. A geometric sequence is one where every term
but the first is the product of the previous term and some constant.
More formally:
Let A(i) denote the ith term in the sequence.
A geometric sequence is a sequence of numbers such that for every i > 0, A(i) =
c * A(i-1) for some constant c.
An arithmetic sequence is a sequence of numbers such that for every i > 0,
A(i+1) = c + A(i) for some constant c.
In both sequences there are no constraints on the value of A(0), as it is given
as the first term in the sequence.
Your task is, given an arbitrary sequence of numbers, you must determine
whether it could be an arithmetic sequence, a geometric sequence, neither, or
both. The input will consist of a sequence of integers. Your method will
output one of the following (quotes are for clarity only):
"GEOMETRIC"
"ARITHMETIC"
"BOTH"
"NEITHER"
DEFINITION
Class: ArithOrGeo
Method: getType
Parameters: int[]
Returns: String
Method signature (be sure your method is public): String getType(int[]
sequence);
NOTES:
- outputs are case sensitive
- 0 is an acceptable value for the constant in either series
- the constants do not have to be the same for the series to be "BOTH"
- The constant multiplier for a geometric sequence is not neccessarily an integer
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- all terms in sequence will be integers in the range -1000 to 1000, inclusive
- sequence will contain between 1 and 50 elements, inclusive
EXAMPLES
1. sequence = {1,2,3,4}
This is an arithmetic sequence because each term is 1 more than the previous term
returns "ARITHMETIC"
2. sequence = {1,2,4,8}
This is a geometric sequence because each term is 2 times the previous term
returns "GEOMETRIC"
3. sequence = {2}
returns "BOTH"
4. sequence = {1,4,9,16}
returns "NEITHER"
5. sequence = {-2,1}
returns "BOTH"
6. sequence = {5,0,0}
returns "GEOMETRIC"
7. sequence = {0,0,5}
returns "NEITHER"
8. sequence = {27,18,12,8}
returns "GEOMETRIC"
9. sequence = {0,5}
returns "ARITHMETIC"