Check if X can be converted to Y by converting to 3 * (X / 2) or X - 1 in each operation
Last Updated :
11 Jun, 2021
Given two positive integers X and Y, the task is to check if X can be converted to Y or not by repeatedly changing the value of X to (3 * X / 2) (if X is even) or (X - 1). If it is possible to convert X into Y, then print "Yes". Otherwise, print "No".
Examples:
Input: X = 6, Y = 8
Output: Yes
Explanation:
Operation 1: Convert X(= 6) to 3*X/2 ( = 3 * (6 / 2) = 9).
Operation 2: Convert X(= 9) to (X - 1) (= 8).
Therefore, the total number of operations required is 2.
Input: X = 3, Y = 6
Output: No
Approach: The given problem can be solved based on the following observations:
- If the value of X is at least Y, then X can always be converted to Y using the second operation, i.e. reducing X by 1.
- If X is even, then it can be converted to (3 * (X / 2)), which is greater than X for all even numbers greater than 0.
- If the value of X is odd, then X can be converted to (X - 1), which can be converted into (3 * (X - 1)/2), which is greater than X.
- Therefore, from the above observations, the conversion of X into Y is always possible for X > 3.
- Following base cases are required to be considered:
- X = 1: Conversion possible only if Y = 1.
- X = 2 or X = 3: Conversion possible only if Y ? 3.
- In all the other cases, conversion is not possible.
Follow the steps below to solve the problem:
- If the value X is greater than 4, then print "Yes".
- If the value X is 1 and Y is 1, then print "Yes".
- If the value X is 2 or 3 and Y is less than 4, then print "Yes".
- Otherwise, print "No" for all the other cases.
Below is the implementation of above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3) {
cout << "Yes";
}
else if (X == 1 and Y == 1) {
cout << "Yes";
}
else if (X == 2 and Y <= 3) {
cout << "Yes";
}
else if (X == 3 and Y <= 3) {
cout << "Yes";
}
// Otherwise, conversion
// is not possible
else {
cout << "No";
}
}
// Driver Code
int main()
{
int X = 6, Y = 8;
check(X, Y);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
static void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3)
{
System.out.print("Yes");
}
else if (X == 1 && Y == 1)
{
System.out.print("Yes");
}
else if (X == 2 && Y <= 3)
{
System.out.print("Yes");
}
else if (X == 3 && Y <= 3)
{
System.out.print("Yes");
}
// Otherwise, conversion
// is not possible
else
{
System.out.print("No");
}
}
// Driver Code
public static void main (String[] args)
{
int X = 6, Y = 8;
check(X, Y);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to check if X can be
# made equal to Y by converting
# X to (3*X/2) or (X - 1)
def check(X, Y):
# Conditions for possible conversion
if (X > 3):
print("Yes")
elif (X == 1 and Y == 1):
print("Yes")
elif (X == 2 and Y <= 3):
print("Yes")
elif (X == 3 and Y <= 3):
print("Yes")
# Otherwise, conversion
# is not possible
else:
print("No")
# Driver Code
if __name__ == '__main__':
X = 6
Y = 8
check(X, Y)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
static void check(int X, int Y)
{
// Conditions for possible conversion
if (X > 3)
{
Console.WriteLine("Yes");
}
else if (X == 1 && Y == 1)
{
Console.WriteLine("Yes");
}
else if (X == 2 && Y <= 3)
{
Console.WriteLine("Yes");
}
else if (X == 3 && Y <= 3)
{
Console.WriteLine("Yes");
}
// Otherwise, conversion
// is not possible
else
{
Console.WriteLine("No");
}
}
// Driver Code
public static void Main(string[] args)
{
int X = 6, Y = 8;
check(X, Y);
}
}
// This code is contributed by avijitmondal1998
JavaScript
<script>
// JavaScript program for the above approach
// Function to check if X can be
// made equal to Y by converting
// X to (3*X/2) or (X - 1)
function check(X, Y)
{
// Conditions for possible conversion
if (X > 3)
{
document.write("Yes");
}
else if (X == 1 && Y == 1)
{
document.write("Yes");
}
else if (X == 2 && Y <= 3)
{
document.write("Yes");
}
else if (X == 3 && Y <= 3)
{
document.write("Yes");
}
// Otherwise, conversion
// is not possible
else
{
document.write("No");
}
}
let X = 6, Y = 8;
check(X, Y);
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if two coordinates can be made equal by incrementing/decrementing by K1 and K2 respectively Given two integer coordinates (X1, Y1) and (X2, Y2) and two positive integers K1 and K2, the task is to check if both the coordinates can be made equal by performing the following steps any number of times: Add or subtract K1 from either or both coordinates of (X1, Y1).Add or subtract K2 from either
7 min read
Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 Given an integer X, the task is to convert 1 into X by using the below-given operations: Multiply the number by 2.Multiply the number by 3.Add 1 to the number. The task is to print the minimum number of operations needed to convert 1 into X using these three operations and also print the sequence of
10 min read
Minimize operations to convert A to B by adding any odd integer or subtracting any even integer Given two positive integers A and B. The task is to find the minimum number of operations required to convert number A into B. In a move, any one of the following operations can be applied on the number A: Select any odd integer x (x>0) and add it to A i.e. (A+x);Or, select any even integer y (y
5 min read
Convert X into Y by repeatedly multiplying X with 2 or appending 1 at the end Given two positive integers X and Y, the task is to check if it is possible to convert the number X into Y, either by multiplying X by 2 or appending 1 at the end of X. If it is possible to convert X into Y, then print "Yes". Otherwise, print "No". Examples: Input: X = 100, Y = 40021Output: YesExpla
8 min read
Minimize cost to convert given two integers to zero using given operations Given two integers X and Y, and two values cost1 and cost2, the task is to convert the given two numbers equal to zero at minimal cost by performing the following two types of operations: Increase or decrease any one of them by 1 at cost1.Increase or decrease both of them by 1 at cost2. Examples: In
6 min read