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

📅  最后修改于: 2021-05-06 19:12:52             🧑  作者: Mango

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

例子:

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

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

高效的方法:可以通过使用Binary Search查找AB来优化上述解决方案。请按照以下步骤解决问题:

  • 初始化两个变量low = 0high = 10 9
  • 迭代直到(高–低)大于1并执行以下操作:
    • 找到中间中档(低+高)/ 2的值。
    • 现在,比较√N与中间元素中旬,如果√N小于或等于中间元素,则高达中旬
    • 否则,更新为
  • 完成上述所有步骤后,将A =高
  • 重复相同的过程以找到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


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

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

Java脚本


输出:
7

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