This a question that I got in a coding exam and couldn’t solve it at that time. The question states that,
You are currently at cell (1, 1) of an N X M grid. There is a rule that decides how you can move in the grid to reach the position (N, M). The rule is, that if you are at cell (x, y) then from there you can either move to cell (x, x+y) or to cell (x+y, y) in one step.
Your task is to find the minimum number of steps that you must take to reach cell (N, M) starting from current position i.e. (1, 1)
Note: If it is not possible to reach (N, M) from (1, 1), then return-1 as your output
input1: An integer value representing the value of N where 1<=N input2: An integer value representing the value of M where M< 106
Return the minimum number of steps.
(1, 1)-> (1, 2)-> (3, 2)
The number of steps involved are 2 only. Therefore, 2 will be returned as the output
Source: Windows Questions C++