The problem is from a programming competition.
You are given two sequences A (a1, a2, a3, ... an) and B (b1, b2, b3, ... bn) of the same length N. In each step you can set ai = ai - bi if ai >= bi. Determine the minimum number of steps required to make all the numbers in A equal to each other.
For Example
A = {5, 7, 10, 5, 15}
B = {2, 2, 1, 3, 5}
In the above example, the minimum number of steps required to make all the elements in A equal to each other is 8.
A = {5, 15, 25, 35, 45, 90, 100}
B = {5, 5, 5, 5, 5, 5, 5}
In the above example, the minimum number of steps required to make all the elements in A equal to each other is 56.
Do note that if it is not possible to make all the elements of the sequence A equal to each other then print -1.
For Example
A = {5, 6}
B = {4, 3}
In the above case, the answer is -1 because it is impossible to make all the elements of A equal to each other.
A = {5, 15, 25, 35, 45, 90, 100}
B = {5, 5, 5, 5, 5, 0, 5}
In the above case, the answer is -1 because it is impossible to make all the elements of A equal to each other.
Could anyone can help me out on how to solve this problem efficiently?