Minimal Divisibility Pair

Easy
0/40
0 upvote

Problem statement

You are given two positive integers, 'a' and 'b'.

Your task is to find the smallest possible sum of x + y, where x and y are non-negative integers that you can add to 'a' and 'b' respectively, such that the resulting number (b + y) is perfectly divisible by the resulting number (a + x).


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains two space-separated integers, 'a' and 'b'.


Output Format:
Print a single integer representing the minimum possible value of x + y.


Note:
x and y must be greater than or equal to 0.
The condition to satisfy is (b + y) % (a + x) == 0.
Sample Input 1:
6 10


Sample Output 1:
2


Explanation for Sample 1:
We need to find non-negative `x` and `y` to minimize `x + y` such that `(10 + y) % (6 + x) == 0`.
- Let's try `x = 0`. We need `(10 + y)` to be a multiple of `6`. The smallest multiple of 6 that is >= 10 is 12. So, `10 + y = 12` which gives `y = 2`. The sum `x + y = 0 + 2 = 2`.
- Let's try `y = 0`. We need `10` to be a multiple of `6 + x`. The smallest number >= 6 that divides 10 is 10 itself. So, `6 + x = 10` which gives `x = 4`. The sum `x + y = 4 + 0 = 4`.
The minimum sum found is 2.


Sample Input 2:
4 12


Sample Output 2:
0


Explanation for Sample 2:
We are given a=4 and b=12.
If we choose `x = 0` and `y = 0`, we get:
- `a + x = 4`
- `b + y = 12`
Since `12 % 4 == 0`, the condition is already satisfied. The minimum sum `x + y` is 0.


Expected Time Complexity:
The expected time complexity is O(sqrt(B))


Constraints:
1 <= a, b <= 1000

Time limit: 1sec
Approaches (1)
Minimal Divisibility Pair
Time Complexity

The expected time complexity is O(sqrt(B))

Space Complexity
Code Solution
(100% EXP penalty)
Minimal Divisibility Pair
Full screen
Console