Given two integers M & N, find minimum steps to reach M*N from (1,1), where condition of traversal is (x+y,y) or (x,y+x) and, x,y initially are (1,1) [closed]

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

Input Specification:

input1: An integer value representing the value of N where 1<=N input2: An integer value representing the value of M where M< 106

Output Specification:

Return the minimum number of steps.

Example 1:

input1: 3

input2: 2

Output: 2

Explanation:

(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++

One Reply to “Given two integers M & N, find minimum steps to reach M*N from (1,1), where condition of traversal is (x+y,y) or (x,y+x) and, x,y initially are (1,1) [closed]”

LEAVE A COMMENT