Given two integers **X** and **Y**, the task is to convert **X** to **Y** using the following operations:

- Add any prime number to
**X**. - Subtract any prime number from
**Y**.

Print the maximum number of such operations required or **-1** if it is not possible to convert **X** to **Y**.

**Examples:**

Input:X = 2, Y = 4

Output:1

2 -> 4

Input:X = 5, Y = 6

Output:-1

It is impossible to convert 5 to 6

with the given operations.

**Approach:** As the task is to maximize the operations, so the minimum possible value must be added to **X** in every operation. Since the value has to be prime, so the minimum two primes i.e. **2** and **3** can be used as they both are prime and can cover both even and odd parity. Now, there are three cases:

- If
**X > Y**then the answer will be**-1**as**X**cannot be made equal to**Y**with the given operation. - If
**X = Y**then the answer will be**0**. - If
**X < Y**then calculate**P = Y – X**and,- If
**P = 1**then the answer will be**-1**as**1**is not prime and it cannot be added or subtracted. - If
**P**is even then**2**can be repetedly added to X and the answer will be**P / 2** - If
**P**is even then add**3**to**X**and then**2**can again be repeatedly added to the new**X**to make it equal to**Y**, the result in this case will be**1 + ((P – 3) / 2)**.

- If

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum operations ` `// required to convert X to Y ` `int` `maxOperations(` `int` `X, ` `int` `Y) ` `{ ` ` ` ` ` `// X cannot be converted to Y ` ` ` `if` `(X > Y) ` ` ` `return` `-1; ` ` ` ` ` `int` `diff = Y - X; ` ` ` ` ` `// If the differecne is 1 ` ` ` `if` `(diff == 1) ` ` ` `return` `-1; ` ` ` ` ` `// If the difference is even ` ` ` `if` `(diff % 2 == 0) ` ` ` `return` `(diff / 2); ` ` ` ` ` `// Add 3 to X and the new ` ` ` `// difference will be even ` ` ` `return` `(1 + ((diff - 3) / 2)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 5, Y = 16; ` ` ` ` ` `cout << maxOperations(X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum operations ` `// required to convert X to Y ` `static` `int` `maxOperations(` `int` `X, ` `int` `Y) ` `{ ` ` ` ` ` `// X cannot be converted to Y ` ` ` `if` `(X > Y) ` ` ` `return` `-` `1` `; ` ` ` ` ` `int` `diff = Y - X; ` ` ` ` ` `// If the differecne is 1 ` ` ` `if` `(diff == ` `1` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// If the difference is even ` ` ` `if` `(diff % ` `2` `== ` `0` `) ` ` ` `return` `(diff / ` `2` `); ` ` ` ` ` `// Add 3 to X and the new ` ` ` `// difference will be even ` ` ` `return` `(` `1` `+ ((diff - ` `3` `) / ` `2` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `X = ` `5` `, Y = ` `16` `; ` ` ` ` ` `System.out.println(maxOperations(X, Y)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum operations ` `# required to convert X to Y ` `def` `maxOperations(X, Y) : ` ` ` ` ` `# X cannot be converted to Y ` ` ` `if` `(X > Y) : ` ` ` `return` `-` `1` `; ` ` ` ` ` `diff ` `=` `Y ` `-` `X; ` ` ` ` ` `# If the differecne is 1 ` ` ` `if` `(diff ` `=` `=` `1` `) : ` ` ` `return` `-` `1` `; ` ` ` ` ` `# If the difference is even ` ` ` `if` `(diff ` `%` `2` `=` `=` `0` `) : ` ` ` `return` `(diff ` `/` `/` `2` `); ` ` ` ` ` `# Add 3 to X and the new ` ` ` `# difference will be even ` ` ` `return` `(` `1` `+` `((diff ` `-` `3` `) ` `/` `/` `2` `)); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `5` `; Y ` `=` `16` `; ` ` ` ` ` `print` `(maxOperations(X, Y)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum operations ` `// required to convert X to Y ` `static` `int` `maxOperations(` `int` `X, ` `int` `Y) ` `{ ` ` ` ` ` `// X cannot be converted to Y ` ` ` `if` `(X > Y) ` ` ` `return` `-1; ` ` ` ` ` `int` `diff = Y - X; ` ` ` ` ` `// If the differecne is 1 ` ` ` `if` `(diff == 1) ` ` ` `return` `-1; ` ` ` ` ` `// If the difference is even ` ` ` `if` `(diff % 2 == 0) ` ` ` `return` `(diff / 2); ` ` ` ` ` `// Add 3 to X and the new ` ` ` `// difference will be even ` ` ` `return` `(1 + ((diff - 3) / 2)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `X = 5, Y = 16; ` ` ` ` ` `Console.WriteLine(maxOperations(X, Y)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count minimum moves required to convert A to B
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Array element moved by k using single moves
- Number of triangles after N moves
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Find the index of the left pointer after possible moves in the array
- Minimum number of moves required to reach the destination by the king in a chess board
- Number of moves required to guess a permutation.
- Total position where king can reach on a chessboard in exactly M moves
- Count possible moves in the given direction in a grid
- Minimum number of given moves required to make N divisible by 25
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Count number of 1s in the array after N moves
- Check if the robot is within the bounds of the grid after given moves
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Minimum number of moves to reach N starting from (1, 1)
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.