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; // Function to whether it is possible // to make X a multiple of Y void check( int X, int Y) { int d = Y - X; if (d == 0 || X <= d) cout << ( "Yes" ); else cout << ( "No" ); } // Driver code int main() { int X = 7; int Y = 15; // Function Call check(X, Y); } // This code is contributed by garg28harsh. |
Java
// Java code to implement the approach import java.io.*; import java.util.*; class GFG { // Function to whether it is possible // to make X a multiple of Y 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" ); } // Driver code public static void main(String[] args) { int X = 7 ; int Y = 15 ; // Function Call check(X, Y); } } |
Python3
# Python code to implement the approach # Function to whether it is possible # to make X a multiple of Y def check(X, Y): d = Y - X if (d = = 0 or X < = d): print ( "Yes" ) else : print ( "No" ) # Driver code X = 7 Y = 15 # Function Call check(X, Y) # This code is contributed by Pushpesh Raj. |
C#
// C# code to implement the approach using System; public class GFG { // Function to whether it is possible // to make X a multiple of Y public static void check( int X, int Y) { int d = Y - X; if (d == 0 || X <= d) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } // Driver code public static void Main( string [] args) { int X = 7; int Y = 15; // Function Call check(X, Y); } } // This code is contributed by AnkThon |
Javascript
// JavaScript code for the above approach // Function to whether it is possible // to make X a multiple of Y function check(X, Y) { let d = Y - X; if (d == 0 || X <= d) console.log( "Yes" ); else console.log( "No" ); } // Driver code let X = 7; let Y = 15; // Function Call check(X, Y); // This code is contributed by Potta Lokesh |
Yes
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; // Function to whether it is possible // to make X a multiple of Y void check( int X, int Y) { for ( int Z = 1; Z <= Y; Z++) { if ((X + Z) % Y == 0) { cout << "Yes" ; return ; } } cout << "No" ; } // Driver code int main() { int X = 7; int Y = 15; // Function Call check(X, Y); return 0; } |
Java
// JAVA CODE FOR ABOVE APPROACH public class Main { // Function to check whether it is possible // to make X a multiple of Y 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" ); } // Driver code public static void main(String[] args) { int X = 7 ; int Y = 15 ; // Function Call check(X, Y); } } // This code is contributed by Aditi Tyagi |
Python3
# Python code for the approach 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 # Function Call check(X, Y) #This code is contributed by Aditi Tyagi |
C#
using System; class Program { // Function to check whether it is possible // to make X a multiple of Y 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" ); } // Driver code static void Main() { int X = 7; int Y = 15; // Function Call Check(X, Y); } } |
Javascript
// Function to whether it is possible // to make X a multiple of Y function check(X, Y) { for (let Z = 1; Z <= Y; Z++) { // Checking mod value of x + z with y if ((X + Z) % Y === 0) { console.log( "Yes" ); return ; } } console.log( "No" ); } // Driver code const X = 7; const Y = 15; check(X, Y); |
Yes
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.