📌  相关文章
📜  乘积严格大于 N 的两个整数的最小和

📅  最后修改于: 2021-10-26 06:37:37             🧑  作者: Mango

给定一个整数N ,任务是找到两个具有最小可能和的整数,使得它们的乘积严格大于N

例子:

朴素的方法:让所需的数字是AB 。这个想法是基于观察,为了最小化它们的总和, A应该是大于√N的最小数。一旦找到AB将等于A×B > N的最小数,这可以线性地找到。

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

高效的方法:可以通过使用二分搜索来优化上述解决方案来找到AB 。请按照以下步骤解决问题:

  • 初始化两个变量low = 0high = 10 9
  • 迭代直到(高 – 低)大于 1 并执行以下操作:
    • 中档 mid的值为(low + high)/2
    • 现在,将√N与中间元素mid进行比较,如果√N小于或等于中间元素,则mid
    • 否则,将更新为mid
  • 完成上述所有步骤后,设置A = high
  • 重复相同的过程以找到B使得A×B > N
  • 完成上述步骤后,打印AB和作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
#define ll long long int
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
void minSum(int N)
{
    // Initialise low as 0 and
    // high as 1e9
    ll low = 0, high = 1e9;
 
    // Iterate to find the first number
    while (low + 1 < high) {
 
        // Find the middle value
        ll mid = low + (high - low) / 2;
 
        // If mid^2 is greater than
        // equal to A, then update
        // high to mid
        if (mid * mid >= N) {
            high = mid;
        }
 
        // Otherwise update low
        else {
            low = mid;
        }
    }
 
    // Store the first number
    ll first = high;
 
    // Again, set low as 0 and
    // high as 1e9
    low = 0;
    high = 1e9;
 
    // Iterate to find the second number
    while (low + 1 < high) {
 
        // Find the middle value
        ll mid = low + (high - low) / 2;
 
        // If first number * mid is
        // greater than N then update
        // high to mid
        if (first * mid > N) {
            high = mid;
        }
 
        // Else, update low to mid
        else {
            low = mid;
        }
    }
 
    // Store the second number
    ll second = high;
 
    // Print the result
    cout << first + second;
}
 
// Driver Code
int main()
{
    int N = 10;
 
    // Function Call
    minSum(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Initialise low as 0 and
    // high as 1e9
    long low = 0, high = 1000000000;
 
    // Iterate to find the first number
    while (low + 1 < high)
    {
         
        // Find the middle value
        long mid = low + (high - low) / 2;
 
        // If mid^2 is greater than
        // equal to A, then update
        // high to mid
        if (mid * mid >= N)
        {
            high = mid;
        }
 
        // Otherwise update low
        else
        {
            low = mid;
        }
    }
 
    // Store the first number
    long first = high;
 
    // Again, set low as 0 and
    // high as 1e9
    low = 0;
    high = 1000000000;
 
    // Iterate to find the second number
    while (low + 1 < high)
    {
         
        // Find the middle value
        long mid = low + (high - low) / 2;
 
        // If first number * mid is
        // greater than N then update
        // high to mid
        if (first * mid > N)
        {
            high = mid;
        }
 
        // Else, update low to mid
        else
        {
            low = mid;
        }
    }
 
    // Store the second number
    long second = high;
 
    // Print the result
    System.out.println(first + second);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Function to find the minimum sum of
# two integers such that their product
# is strictly greater than N
def minSum(N):
     
    # Initialise low as 0 and
    # high as 1e9
    low = 0
    high = 1000000000
 
    # Iterate to find the first number
    while (low + 1 < high):
         
        # Find the middle value
        mid = low + (high - low) / 2
 
        # If mid^2 is greater than
        # equal to A, then update
        # high to mid
        if (mid * mid >= N):
            high = mid
 
        # Otherwise update low
        else:
            low = mid
 
    # Store the first number
    first = high
 
    # Again, set low as 0 and
    # high as 1e9
    low = 0
    high = 1000000000
 
    # Iterate to find the second number
    while (low + 1 < high):
 
        # Find the middle value
        mid = low + (high - low) / 2
 
        # If first number * mid is
        # greater than N then update
        # high to mid
        if (first * mid > N):
            high = mid
 
        # Else, update low to mid
        else:
            low = mid
 
    # Store the second number
    second = high
 
    # Print the result
    print(round(first + second))
 
# Driver Code
N = 10
 
# Function Call
minSum(N)
 
# This code is contributed by Dharanendra L V


C#
// C# program for the above approach
using System;
 
class GFG{
   
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Initialise low as 0 and
    // high as 1e9
    long low = 0, high = 1000000000;
 
    // Iterate to find the first number
    while (low + 1 < high)
    {
         
        // Find the middle value
        long mid = low + (high - low) / 2;
 
        // If mid^2 is greater than
        // equal to A, then update
        // high to mid
        if (mid * mid >= N)
        {
            high = mid;
        }
 
        // Otherwise update low
        else
        {
            low = mid;
        }
    }
 
    // Store the first number
    long first = high;
 
    // Again, set low as 0 and
    // high as 1e9
    low = 0;
    high = 1000000000;
 
    // Iterate to find the second number
    while (low + 1 < high)
    {
         
        // Find the middle value
        long mid = low + (high - low) / 2;
 
        // If first number * mid is
        // greater than N then update
        // high to mid
        if (first * mid > N)
        {
            high = mid;
        }
 
        // Else, update low to mid
        else
        {
            low = mid;
        }
    }
 
    // Store the second number
    long second = high;
 
    // Print the result
    Console.WriteLine( first + second);
}
 
// Driver Code
static public void Main()
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V


Javascript


C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
void minSum(int N)
{
    // Store the answer using the
    // AP-GP inequality
    int ans = ceil(2 * sqrt(N + 1));
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 10;
 
    // Function Call
    minSum(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.lang.*;
 
class GFG{
     
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Store the answer using the
    // AP-GP inequality
    int ans = (int)Math.ceil(2 * Math.sqrt(N + 1));
 
    // Print the answer
    System.out.println( ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
import math
 
# Function to find the minimum sum of
# two integers such that their product
# is strictly greater than N
def minSum(N):
     
    # Store the answer using the
    # AP-GP inequality
    ans = math.ceil(2 * math.sqrt(N + 1))
     
    # Print the result
    print(math.trunc(ans))
 
# Driver Code
N = 10
 
# Function Call
minSum(N)
 
# This code is contributed by Dharanendra L V


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Store the answer using the
    // AP-GP inequality
    int ans = (int)Math.Ceiling(2 * Math.Sqrt(N + 1));
 
    // Print the answer
    Console.WriteLine( ans);
}
 
// Driver Code
static public void Main()
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
7

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

最有效的方法:为了优化上述方法,该想法基于算术和几何级数的不等式,如下所示。

以下是上述方法的程序:

C++14

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
void minSum(int N)
{
    // Store the answer using the
    // AP-GP inequality
    int ans = ceil(2 * sqrt(N + 1));
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 10;
 
    // Function Call
    minSum(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.lang.*;
 
class GFG{
     
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Store the answer using the
    // AP-GP inequality
    int ans = (int)Math.ceil(2 * Math.sqrt(N + 1));
 
    // Print the answer
    System.out.println( ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V

蟒蛇3

# Python3 program for the above approach
import math
 
# Function to find the minimum sum of
# two integers such that their product
# is strictly greater than N
def minSum(N):
     
    # Store the answer using the
    # AP-GP inequality
    ans = math.ceil(2 * math.sqrt(N + 1))
     
    # Print the result
    print(math.trunc(ans))
 
# Driver Code
N = 10
 
# Function Call
minSum(N)
 
# This code is contributed by Dharanendra L V

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
static void minSum(int N)
{
     
    // Store the answer using the
    // AP-GP inequality
    int ans = (int)Math.Ceiling(2 * Math.Sqrt(N + 1));
 
    // Print the answer
    Console.WriteLine( ans);
}
 
// Driver Code
static public void Main()
{
    int N = 10;
     
    // Function Call
    minSum(N);
}
}
 
// This code is contributed by Dharanendra L V

Javascript


输出:
7

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程