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?
1
votes
3 Answers
7
votes
1
votes
we have to find any number such that A % M == B % M
now to satisfy this condition
- we can assume without loss of generality
A > B
thenB = A - (A - B)
- now we take mod on both sides it will become
B % M = (A - (A - B)) % M
- to reduce
(A-B)
from our equation we can replace M with(A-B)
- our equation become
B % (A-B) = (A - (A-B)) % (A-B)
solving it futher it becomesB % (A-B) = (A % (A-B) - (A-B) % (A-B))
as(A-B) % (A-B) is 0
- so our final equation will becomes
B % (A - B) = A % (A - B)
which is same as given equation hereM = (A - B)
is our final answer
M = (A - B) if A > B
M = (B - A) if B > A