Given two numbers X and Y. the duty is to seek out the quantity N (N ≤ 10^18) which satisfies these two circumstances:
- (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 may be solved based mostly on the next thought:
We will implement the straightforward math idea of the quantity system.
- N + X = Y …………(i)
- N – Y = X …………(ii)
Normalizes the equation we will get N could also be equal to (X*Y – X + Y). Additionally, it’s satisfying these two circumstances. So, the reply is (X * Y – X + Y).
Under is the implementation of the above method:
C++
// C++ code for the above method: #embody <iostream> utilizing namespace std; #outline int lengthy lengthy int // Perform to seek out N int FindN(int x, int y) { // Mathematical equation from method int N = (x * y) - x + y; // Return N return N; } // Driver code signed primary() { int X = 18, Y = 42; // Perform name cout << FindN(X, Y); return 0; }
Time Complexity: O(1)
Auxiliary Area: O(1)