Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a and b, find m such that a mod m = b mod m

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?

like image 758
Ayush Lodha Avatar asked Oct 24 '25 06:10

Ayush Lodha


2 Answers

          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 .

like image 120
D Stanley Avatar answered Oct 27 '25 00:10

D Stanley


we have to find any number such that A % M == B % M now to satisfy this condition

  1. we can assume without loss of generality A > B then B = A - (A - B)
  2. now we take mod on both sides it will become B % M = (A - (A - B)) % M
  3. to reduce (A-B) from our equation we can replace M with (A-B)
  4. our equation become 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 0
  5. so our final equation will becomes B % (A - B) = A % (A - B) which is same as given equation here M = (A - B) is our final answer
M = (A - B) if A > B 
M = (B - A) if B > A
like image 26
Vikas Sharma Avatar answered Oct 26 '25 22:10

Vikas Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!