📜  最大素数移动将X转换为Y

📅  最后修改于: 2021-05-08 17:32:06             🧑  作者: Mango

给定两个整数XY ,任务是使用以下操作将X转换为Y

  1. 将任何质数加到X。
  2. Y减去任何质数。

打印所需的最大此类操作数;如果无法将X转换为Y ,则打印-1

例子:

方法:由于任务是使操作最大化,因此必须在每个操作中将最小可能值添加到X。由于该值必须是质数,因此可以使用最少的两个质数,即23,因为它们都是质数,并且可以覆盖偶数和奇数奇偶校验。现在,有三种情况:

  • 如果X> Y,则答案将为-1,因为在给定的操作下X不能等于Y。
  • 如果X = Y,则答案将为0
  • 如果X 则计算P = Y – X,并且,
    • 如果P = 1,则答案将为-1,因为1不是质数,因此无法相加或相减。
    • 如果P为偶数,则可以将2重复添加到X,答案将为P / 2
    • 如果P等于偶数,则在X上3 ,然后可以再次将2重复添加到新的X上,使其等于Y ,这种情况下的结果将是1 +((P – 3)/ 2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


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


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


输出:
5

时间复杂度: O(1)