Problem · Math
Reach a Given Number
Learn this problemProblem statement
You are given three integers: A, B, and N. Your task is to determine whether it is possible to reach the value N starting from either A or B using one of the following operations:
- Replace
AwithA + B. - Replace
BwithA + B.
If it is possible to reach N, return the minimum number of operations required as a string. If it is not possible, return NOT POSSIBLE.
Function
reachGivenNumber(A: long, B: long, N: long) → StringExamples
Example 1
A = 1B = 2N = 5return = "2"(1, 2) -> (3, 2) -> (3, 5)
Example 2
A = -1B = 0N = 5return = "NOT POSSIBLE"(-1, 0) -> (-1, 0)
States are either remain unchanged or produce negative value for both A and B.
Constraints
-10000000000 <= A, B, N <= 10000000000