Problem · Math

Minimum Extra Chocolates

MediumOA

Given two boxes of chocolate containing A and B chocolates representing the number of chocolates Alex and Drake have, respectively. Drake is the younger child and will only be happy if he receives a multiple of what Alex gets. You decided to buy some extra chocolates.

Mathematically, The share of chocolates will be: k * (A + X) = (B + Y) for some non-negative integer X and Y and some positive integer k.

What is the minimum number of extra chocolates (X + Y) that you need to buy?

Note: The original chocolates (A and B) cannot be transferred among each other.

Function Description

Complete the function solution. The function takes 2 parameters and returns the solution:

  • A: Represents the number of initial chocolates in Alex's box
  • B: Represents the number of initial chocolates in Drake's box

Limits

Time Limit: 1.0 sec(s) for each input file
Memory Limit: 256 MB
Source Limit: 1024 KB

Scoring

Score is assigned if any testcase passes

Allowed Languages

[List of programming languages]

Note:

Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.

Constraints

0 < A,B < 10^9

⊹ ࣪ ﹏𓊝﹏𓂁﹏⊹ ࣪Endless thanks to spike!𓂃 𓈒𓏸

Examples
01 · Example 1
A = 8
B = 16
return = 0

For the given test case, if you let X=0 and Y=0, then B+Y=16 will be a multiple of A+X=8. Here, you have X+Y=0, and there is no way to make X+Y smaller, so the answer is 0.

02 · Example 2
A = 7
B = 37
return = 4

Adding 1 to A and 3 to B, we get A as 8 and B as 40, which satisfies the condition as k*A=B. k will be 5 here.

03 · Example 3
A = 70
B = 229
return = 9

X will be 7 and Y will 2, k will end up becoming 3.

Constraints
0 < A,B < 10^9
drafts saved locally
public int solution(int A, int B) {
  // write your code here
}
A8
B16
expected0
sign in to submit