Given two integers X and Y, the task is to check if these two integers can be made equal to 0 by using the given operation any number of times. An operation is described as follows –
- Choose an arbitrary integer Z.
- Update the values with either of the following values:
- X = X – 2*Z and Y = Y – 3*Z
- X = X – 3*Z and Y = Y – 2*Z
Examples:
Input: X = 6, Y = 9
Output: Yes
Explanation:
Operation 1: Choose Z = 3, X = 6 – 2*3 = 0 and Y = 9 – 3*3 = 0
Since X and Y can be made equal to 0 using 1 operation, the required answer is Yes.
Input: X = 33, Y = 27
Output: Yes
Explanation:
Operation 1: Choose Z = 9, X := 33 – 3*9 = 6 and Y := 27 – 2*9 = 9
Operation 2: Choose Z = 3, X := 6 – 2*3 = 0 and Y := 9 – 3*3 = 0
Since X and Y can be made equal to 0 using 2 operation, the required answer is Yes.
Approach: Let’s assume X ? Y. Then the answer is Yes if two following conditions holds:
- (X + Y) mod 5 = 0: because after each operation the value (X + Y) mod 5 does not change.
Let’s assume some arbitrary number Z has been chosen.
Therefore, the value (X + Y) will be changed to
((X - 3Z) + (Y - 2Z))
(X + Y - 5Z)
- For this value to be equal to 0, X + Y = 5Z. Therefore, on taking mod on both the sides, (X + Y) mod 5 has to be equal to 0.
- 3*X >= 2*Y so that the subtraction doesnt make the values of X and Y negative.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ifPossible( int X, int Y)
{
if (X > Y)
swap(X, Y);
if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int X = 33, Y = 27;
ifPossible(X, Y);
return 0;
}
|
Java
class GFG
{
static void ifPossible( int X, int Y)
{
if (X > Y)
swap(X, Y);
if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
static void swap( int x, int y)
{
int temp = x;
x = y;
y = temp;
}
public static void main(String[] args)
{
int X = 33 , Y = 27 ;
ifPossible(X, Y);
}
}
|
Python3
def ifPossible(X, Y):
if (X > Y):
X, Y = Y, X
if ((X + Y) % 5 = = 0 and 3 * X > = 2 * Y):
print ( "Yes" )
else :
print ( "No" )
X = 33
Y = 27
ifPossible(X, Y)
|
C#
using System;
class GFG
{
static void ifPossible( int X, int Y)
{
if (X > Y)
swap(X, Y);
if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void swap( int x, int y)
{
int temp = x;
x = y;
y = temp;
}
public static void Main()
{
int X = 33, Y = 27;
ifPossible(X, Y);
}
}
|
Javascript
<script>
function ifPossible(X, Y)
{
if (X > Y)
{
var temp = X;
X = Y;
Y =temp;
}
if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
document.write( "Yes" );
else
document.write( "No" );
}
var X = 33, Y = 27;
ifPossible(X, Y);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Dec, 2021
Like Article
Save Article