Find The Number N, Where (N+X) Divisible By Y And (N-Y) Divisible By X

In this tutorial, we will learn how to determine the number N, where (N + X) divisible by Y and (N-Y) divisible by X.

This is Two numbers, X and Y, are given. Finding the number N(N ≤ 10^18) that meets these two requirements: (N + X) can be divided by Y, and (N - Y) can be divided by X.

Examples:


The following concept can be used to solve the problem.

The number system is a straightforward mathematical idea that we can use.

Normalizing the equation allows us to obtain N could be the same as (X*Y - X + Y). Additionally, it meets these two requirements. The solution is therefore (X * Y - X + Y).

The application of the aforementioned strategy is seen below:

C++ Program 1:

Output

780

C++ Program 2:

Output

384

Java Code 1:

Output

780

Java Code 2:

Output

384

C# Code 1:

Output

780

C# Code 2:

Output

384
  • Time Complexity will be O(1).
  • Auxiliary Space will be O(1).

So that's the end of the article. I sincerely hope you find this post to be educational and useful.






Latest Courses