Problem · Math

Reach a Given Number

Learn this problem
MediumHypervergeOA

Problem 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:

  1. Replace A with A + B.
  2. Replace B with A + 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) → String

Examples

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
drafts saved locally
public String reachGivenNumber(long A, long B, long N) {
  // write your code here
}
A1
B2
N5
expected"2"
checking account