Optimal approach to solve this math problem

Samle Input:
3 5 2 2
Sample Output:

t-> time taken to meet
p-> count of man stretches
q-> count of woman stretches

so they meet at time t:-
  t-k-a*p = t-n-m*q
 After solving:  k-n = m*q-a*p
 Apply eucledian gcd : if k-n is not divisible by gcd(m,a) answer is impossible 
                       else find p and q;
 How to find q :- (k-n+a*p)/m=q increase p till you find a non fractional number q.
 Either put p in k+a*p, or put q in n+m*q; 

As i have not submitted it, i am not 100% sure that this solution is correct .

1 Like

Thank you :slight_smile: