
findTheFormula
Consider a sequence in which each element is obtained from the previous one by applying some function f
to it. Given three consecutive elements from this sequence as an array seq
, your task is to find and return the function f
used to build it.
Function f
in the format k*n[+/-]b
. If b
is equal to zero, it should be omitted. If the absolute value of k
is 1
, it should be omitted as well.
It is guaranteed that the given function is not a constant and it not an identity function.
Example:
- For
seq = [7,8,9]
, the output should befindTheFormula(seq) ="n+1"
.
Let f = n + 1
. In this case f(7) = 8
, and f(8) = 9
, which coincides with the given elements. Thus, the answer is "n+1"
.
Input/Output:
-
[execution time limit] 0.5 seconds
-
[input] array.integer seq
seq.length = 3
,-106 ≤ seq[i] ≤ 106
. -
[output] string
Functionf
in the formatkn[+-]b
. Ifb
is equal to zero, it should be omitted. If the absolute value ofk
is1
, it should be omitted as well.
Post Comment