Given two numbers X and Y. the duty is to search out the quantity N (N ≤ 10^18) which satisfies these two situations:
- (N + X) is divisible by Y
- (N – Y) is divisible by X
Examples:
Enter: X = 18, Y = 42
Output:780Enter: X = 100, Y = 200
Output: 500
Strategy: The issue could be solved primarily based on the next concept:
We will implement the easy math idea of the quantity system.
- N + X = Y …………(i)
- N – Y = X …………(ii)
Normalizes the equation we are able to get N could also be equal to (X*Y – X + Y). Additionally, it’s satisfying these two situations. So, the reply is (X * Y – X + Y).
Beneath is the implementation of the above method:
C++
|
Time Complexity: O(1)
Auxiliary House: O(1)