Open In App

Maximum prime moves to convert X to Y

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

  1. Add any prime number to X.
  2. 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:
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: 
 

Below is the implementation of the above approach: 
 




// 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 difference 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;
}




// 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 difference 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




# 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 difference 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




// 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 difference 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




<script>
 
// JavaScript implementation of the approach
 
// Function to return the maximum operations
// required to convert X to Y
function maxOperations(X, Y)
{
 
    // X cannot be converted to Y
    if (X > Y)
        return -1;
 
    let diff = Y - X;
 
    // If the difference 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
 
    let X = 5, Y = 16;
 
    document.write(maxOperations(X, Y));
 
// This code is contributed by _saurabh_jaiswal   
 
</script>

Output: 
5

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 


Article Tags :