Select Page

# Find the number N, where (N+X) divisible by Y and (N-Y) divisible by X

Team 1 - Programming
Published: January 27, 2023

Given two numbers X and Y. the task is to find the number N (N ≤ 10^18) which satisfies these two conditions:

• (N + X) is divisible by Y
• (N – Y) is divisible by X

Examples:

Input: X = 18, Y = 42
Output:780

Input: X = 100, Y = 200
Output: 500

Approach: The problem can be solved based on the following idea:

We can implement the simple math concept of the number system.

• N + X = Y    …………(i)
• N – Y = X     …………(ii)

Normalizes the equation we can get N may be equal to (X*Y – X + Y). Also, it is satisfying these two conditions. So, the answer is (X * Y – X + Y).

Below is the implementation of the above approach:

## C++

```// C++ code for the above approach:
#include <iostream>
using namespace std;
#define int long long int

// Function to find N
int FindN(int x, int y)
{

// Mathematical equation from approach
int N = (x * y) - x + y;

// Return N
return N;
}

// Driver code
signed main()
{

int X = 18, Y = 42;

// Function call
cout << FindN(X, Y);
return 0;
}```

Time Complexity: O(1)
Auxiliary Space: O(1)

Source: www.geeksforgeeks.org