Check if Y can be made multiple of X by adding any value to both
Given two numbers X and Y (X ≤ Y), you can select any positive integer Z and add them to both X and Y. The task is to find whether it is possible to make X a multiple of Y.
Examples:
Input: X = 7, Y = 15
Output: Yes
????xplanation: We can choose Z = 1 and add them to 7 and 15. Thus, 7 + 1 = 8 is a factor of 15 + 1 = 16.
Input: X = 9, Y = 10
Output: No
Approach: The problem can be solved based on the following idea:
- First, if X = Y, then the answer is obviously “Yes”.
- Otherwise, note that no matter which Z we choose, the difference between X and Y remains constant.
Let d = Y – X. A valid Z exists if and only if X ≤ d.
Follow the steps mentioned below to implement the above idea:
- First, we find the difference between X and Y let d = Y – X.
- If d = 0 or X ≤ d, then print “Yes”
- Else print “No”
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void check( int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
cout << ( "Yes" );
else
cout << ( "No" );
}
int main()
{
int X = 7;
int Y = 15;
check(X, Y);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void check( int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int X = 7 ;
int Y = 15 ;
check(X, Y);
}
}
|
Python3
def check(X, Y):
d = Y - X
if (d = = 0 or X < = d):
print ( "Yes" )
else :
print ( "No" )
X = 7
Y = 15
check(X, Y)
|
C#
using System;
public class GFG {
public static void check( int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main( string [] args)
{
int X = 7;
int Y = 15;
check(X, Y);
}
}
|
Javascript
function check(X, Y)
{
let d = Y - X;
if (d == 0 || X <= d)
console.log( "Yes" );
else
console.log( "No" );
}
let X = 7;
let Y = 15;
check(X, Y);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach:
One approach to solve this problem is to iterate over all possible values of Z and check if adding Z to X and Y makes X a multiple of Y. If we find such a value of Z, we can return “Yes”, otherwise we return “No”.
- Initialize a loop variable Z to 1.
- While Z is less than or equal to Y, repeat the following steps:
- Check if (X + Z) is divisible by Y.
- If it is divisible, return “Yes” and exit the function.
- If it is not divisible, increment Z by 1 and continue to the next iteration of the loop.
- If the loop completes without finding a suitable value of Z, return “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void check( int X, int Y) {
for ( int Z = 1; Z <= Y; Z++) {
if ((X + Z) % Y == 0) {
cout << "Yes" ;
return ;
}
}
cout << "No" ;
}
int main() {
int X = 7;
int Y = 15;
check(X, Y);
return 0;
}
|
Java
public class Main {
static void check( int X, int Y) {
for ( int Z = 1 ; Z <= Y; Z++) {
if ((X + Z) % Y == 0 ) {
System.out.println( "Yes" );
return ;
}
}
System.out.println( "No" );
}
public static void main(String[] args) {
int X = 7 ;
int Y = 15 ;
check(X, Y);
}
}
|
Python3
def check(X, Y):
for Z in range ( 1 , Y + 1 ):
if (X + Z) % Y = = 0 :
print ( "Yes" )
return
print ( "No" )
if __name__ = = "__main__" :
X = 7
Y = 15
check(X, Y)
|
C#
using System;
class Program
{
static void Check( int X, int Y)
{
for ( int Z = 1; Z <= Y; Z++)
{
if ((X + Z) % Y == 0)
{
Console.WriteLine( "Yes" );
return ;
}
}
Console.WriteLine( "No" );
}
static void Main()
{
int X = 7;
int Y = 15;
Check(X, Y);
}
}
|
Javascript
function check(X, Y) {
for (let Z = 1; Z <= Y; Z++) {
if ((X + Z) % Y === 0) {
console.log( "Yes" );
return ;
}
}
console.log( "No" );
}
const X = 7;
const Y = 15;
check(X, Y);
|
Time Complexity: O(Y), where Y is the value of the second integer. The for loop runs Y times, checking whether X+Z is divisible by Y for each value of Z.
Auxiliary Space: O(1), because we are not using any extra space apart from the input variables and the loop variable.
Last Updated :
21 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...