📜  用给定的总和和最大可能的LCM查找两个数字

📅  最后修改于: 2021-06-26 14:58:05             🧑  作者: Mango

给定一个整数X ,任务是找到两个整数AB ,以使这两个数字之和为XAB的LCM最大。

例子:

天真的方法:最简单的方法是使用两个指针来找到具有给定的总和X和最大可能的LCM的整数对AB。步骤如下:

  • 分别将AB初始化为1X1
  • 循环运行,而A小于等于B。
  • 在每次迭代中,计算ABLCM 然后将A递增1,并将B递减1
  • 打印对应于最大LCMAB。

时间复杂度: O(N)
辅助空间: O(1)

高效的方法:为了优化上述幼稚的方法,其思想是使用一些数学观察结果。两个互质整数的LCM等于两个整数的乘积。因此,该问题可以简化为找到两个互质数整数AB ,使得A + B = X并且A×B最大。步骤如下:

  • 如果X为奇数,则A = floor(X / 2)B = floor(X / 2)+1
  • 否则,如果X是偶数,则
    • 如果floor( X / 2)是偶数,则A = floor(X / 2)– 1B = floor(X / 2)+1
    • 否则,如果floor( X / 2)为奇数,则A = floor(X / 2)– 2B = floor(X / 2)+ 2

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function that print two numbers with
// the sum X and maximum possible LCM
void maxLCMWithGivenSum(int X)
{
    // variables to store the result
    int A, B;
 
    // If X is odd
    if (X & 1) {
        A = X / 2;
        B = X / 2 + 1;
    }
 
    // If X is even
    else {
 
        // If floor(X/2) is even
        if ((X / 2) % 2 == 0) {
            A = X / 2 - 1;
            B = X / 2 + 1;
        }
 
        // If floor(X/2) is odd
        else {
            A = X / 2 - 2;
            B = X / 2 + 2;
        }
    }
 
    // Print the result
    cout << A << " " << B << endl;
}
 
// Driver Code
int main()
{
    // Given Number
    int X = 30;
 
    // Function call
    maxLCMWithGivenSum(X);
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
 
class GFG{
 
// Function that print two numbers with
// the sum X and maximum possible LCM
static void maxLCMWithGivenSum(int X)
{
     
    // Variables to store the result
    int A, B;
 
    // If X is odd
    if ((X & 1) == 1)
    {
        A = X / 2;
        B = X / 2 + 1;
    }
 
    // If X is even
    else
    {
         
        // If floor(X/2) is even
        if ((X / 2) % 2 == 0)
        {
            A = X / 2 - 1;
            B = X / 2 + 1;
        }
 
        // If floor(X/2) is odd
        else
        {
            A = X / 2 - 2;
            B = X / 2 + 2;
        }
    }
     
    // Print the result
    System.out.println(A + " " + B);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given number
    int X = 30;
 
    // Function call
    maxLCMWithGivenSum(X);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function that print two numbers with
# the sum X and maximum possible LCM
def maxLCMWithGivenSum(X):
     
    # If X is odd
    if X % 2 != 0:
        A = X / 2
        B = X / 2 + 1
         
    # If X is even
    else:
         
        # If floor(X/2) is even
        if (X / 2) % 2 == 0:
            A = X / 2 - 1
            B = X / 2 + 1
             
        # If floor(X/2) is odd
        else:
            A = X / 2 - 2
            B = X / 2 + 2
             
    # Print the result
    print(int(A), int(B), end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Number
    X = 30
     
    # Function call
    maxLCMWithGivenSum(X)
 
# This code is contributed by virusbuddah_


C#
// C# program of the above approach
using System;
class GFG{
 
// Function that print two numbers with
// the sum X and maximum possible LCM
static void maxLCMWithGivenSum(int X)
{
     
    // Variables to store the result
    int A, B;
 
    // If X is odd
    if ((X & 1) == 1)
    {
        A = X / 2;
        B = X / 2 + 1;
    }
 
    // If X is even
    else
    {
         
        // If floor(X/2) is even
        if ((X / 2) % 2 == 0)
        {
            A = X / 2 - 1;
            B = X / 2 + 1;
        }
 
        // If floor(X/2) is odd
        else
        {
            A = X / 2 - 2;
            B = X / 2 + 2;
        }
    }
     
    // Print the result
    Console.WriteLine(A + " " + B);
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given number
    int X = 30;
 
    // Function call
    maxLCMWithGivenSum(X);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
13 17

时间复杂度: O(1)
辅助空间: O(1)