Input: P = 3, Q = 27 Output: 6 3 Explanation: Consider the two number as 6, 3. Now, the GCD(6, 3) = 3 and 6*6 – 3*3 = 27 which satisfies the condition.
Input: P = 1, Q = 100 Output: -1
Approach: The given problem can be solved using based on the following observations:
The given equation can also be written as:
Now for an integral solution of the given equation:
(x+y)(x-y) is always an integer => (x+y)(x-y) are divisors of Q
Let (x + y) = p1 and (x + y) = p2 be the two equations where p1 & p2 are the divisors of Q such that p1 * p2 = Q.
Solving for the above two equation we have:
From the above calculations, for x and y to be integral, then the sum of divisors must be even. Since there are 4 possible values for two values of x and y as (+x, +y), (+x, -y), (-x, +y) and (-x, -y). Therefore the total number of possible solution is given by 4*(count pairs of divisors with even sum).
Now among these pairs, find the pair with GCD as P and print the pair. If no such pair exists, print -1.
Below is the implementation of the above approach:
Please Login to comment...