https://www.geeksforgeeks.org/reach-the-numbers-by-making-jumps-of-two-given-lengths/
Given integers k, d1, d2 and an integer array arr[]. Starting from number k you are allowed to make jumps of size d1and d2 i.e. all the possible moves from k are:
- k + d1 and k – d1
- k + d2 and k – d2
The task is to find which of the numbers from the array are reachable from k making any number of jumps and any given jump is either of size d1 or d2. For every number print 1 if its reachable else print 0.
Approach: Any number x that is reachable from k with jumps d1 or d2 will be of the form x = k + (i * d1) + (j * d2)where i and j are integers.
Let the GCD of d1 and d2 be gcd. Since, gcd divides both d1 and d2. Therefore we can write d1 = m1 * gcd and d2 = m2 * gcd where m1 and m2 are integers
And x = k + gcd * (i * m1 + j * m2) = k + M * gcd.
So, any number x that is reachable from k should satisfy (x – k) % gcd = 0.
Let the GCD of d1 and d2 be gcd. Since, gcd divides both d1 and d2. Therefore we can write d1 = m1 * gcd and d2 = m2 * gcd where m1 and m2 are integers
And x = k + gcd * (i * m1 + j * m2) = k + M * gcd.
So, any number x that is reachable from k should satisfy (x – k) % gcd = 0.