I am trying to solve a algorithmic problem, which has a sub-part which asks you to find an integer m such that for given two integers a and b, we get a mod m = b mod m. mod is the modulo operation. How to approach this problem?
a mod m = b mod m
==> (a - b) mod m = 0
==> (a-b) = k * m for some integer k
==> (a-b) / m = k
So m can be any factor of a-b .
we have to find any number such that A % M == B % M
now to satisfy this condition
A > B then B = A - (A - B)B % M = (A - (A - B)) % M(A-B) from our equation we can replace M with (A-B)B % (A-B) = (A - (A-B)) % (A-B) solving it futher it becomes B % (A-B) = (A % (A-B) - (A-B) % (A-B)) as (A-B) % (A-B) is 0B % (A - B) = A % (A - B) which is same as given equation here M = (A - B) is our final answerM = (A - B) if A > B
M = (B - A) if B > A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With