给定一个整数X ,任务是找到两个整数A和B ,以使这两个数字之和为X且A和B的LCM最大。
例子:
Input: X = 15
Output: 7 8
Explanation:
7 + 8 = 15 and LCM(7, 8) = 56 is the maximum possible.
Input: X = 30
Output: 13 17
Explanation:
13 + 17 = 30 and LCM(13, 17) = 221 is the maximum possible.
天真的方法:最简单的方法是使用两个指针来查找具有给定的总和X和最大可能的LCM的整数对A和B。步骤如下:
- 分别将A和B初始化为1和X – 1 。
- 循环运行,而A小于等于B。
- 在每次迭代中,计算A和B的LCM ,然后将A递增1,并将B递减1 。
- 打印对应于最大LCM的A和B。
时间复杂度: O(N)
辅助空间: O(1)
高效的方法:为了优化上述幼稚的方法,其思想是使用一些数学观察结果。两个互质整数的LCM等于两个整数的乘积。因此,该问题可以简化为找到两个互质数整数A和B ,使得A + B = X并且A×B最大。步骤如下:
- 如果X为奇数,则A = floor(X / 2)和B = floor(X / 2)+1 。
- 否则,如果X是偶数,则
- 如果floor( X / 2)是偶数,则A = floor(X / 2)– 1和B = floor(X / 2)+1 。
- 否则,如果floor( X / 2)为奇数,则A = floor(X / 2)– 2和B = 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)