Find the number N, where (N+X) divisible by Y and (N-Y) divisible by X
Last Updated :
05 Apr, 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: 300
Naive Approach/Brute Force Approach:
When constraints are small, then we can use this approach:
In this code, we define a function FindN that takes two integers X and Y as input and returns an integer N that satisfies the two conditions given in the problem. Inside the function, we use a while loop to iterate through all possible values of N until we find a solution or until N exceeds the upper limit of 10^18. For each value of N, we check if both conditions are satisfied using the modulo operator %. If both conditions are satisfied, we return the value of N. If no solution is found, we return -1.
In the main function, we define the values of X and Y and call the FindN function to compute the value of N. The computed value of N is then printed to the console.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define int long long int
int FindN( int x, int y)
{
int N = max(x, y);
while (
N <= 1000000000000000000) {
if ((N + x) % y == 0
&& (N - y) % x
== 0) {
return N;
}
N++;
}
return -1;
}
signed main()
{
int X = 18, Y = 42;
cout << FindN(X, Y);
return 0;
}
|
Java
import java.util.*;
public class Main {
static long findN( long x, long y)
{
long N = Math.max(x, y);
while (N <= 1000000000000000000L) {
if ((N + x) % y == 0
&& (N - y) % x
== 0 ) {
return N;
}
N++;
}
return - 1 ;
}
public static void main(String[] args)
{
long X = 18 , Y = 42 ;
System.out.println(findN(X, Y));
}
}
|
Python3
def FindN(x, y):
N = max (x, y)
while N < = 10 * * 18 :
if (N + x) % y = = 0 and (N - y) % x = = 0 :
return N
N + = 1
return - 1
X, Y = 18 , 42
print (FindN(X, Y))
|
C#
using System;
namespace ConsoleApp
{
class Program
{
static long FindN( long x, long y)
{
long N = Math.Max(x, y);
while (N <= 1000000000000000000)
{
if ((N + x) % y == 0 && (N - y) % x == 0)
{
return N;
}
N++;
}
return -1;
}
static void Main( string [] args)
{
long X = 18, Y = 42;
Console.WriteLine(FindN(X, Y));
}
}
}
|
Javascript
function findN(x, y) {
let N = Math.max(x, y);
while (N <= 1000000000000000000) {
if ((N + x) % y === 0 && (N - y) % x === 0) {
return N;
}
N++;
}
return -1;
}
const X = 18, Y = 42;
console.log(findN(X, Y));
|
Time Complexity: O(1018)
Auxiliary Space: O(1)
Efficient 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++
#include <iostream>
using namespace std;
#define int long long int
int FindN( int x, int y)
{
int N = (x * y) - x + y;
return N;
}
signed main()
{
int X = 18, Y = 42;
cout << FindN(X, Y);
return 0;
}
|
Java
import java.util.*
public class Main {
public static int FindN( int x, int y)
{
int N = (x * y) - x + y;
return N;
}
public static void main(String[] args)
{
int X = 18 , Y = 42 ;
System.out.println(FindN(X, Y));
}
}
|
Python3
def find_n(x, y):
N = (x * y) - x + y
return N
x = 18
y = 42
print (find_n(x, y))
|
C#
using System;
public class GFG {
static int FindN( int x, int y)
{
int N = (x * y) - x + y;
return N;
}
static public void Main()
{
int X = 18, Y = 42;
Console.WriteLine(FindN(X, Y));
}
}
|
Javascript
function FindN(x, y) {
let N = (x * y) - x + y;
return N;
}
let X = 18;
let Y = 42;
console.log(FindN(X, Y));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...