Open In App

Minimum sum of two integers whose product is strictly greater than N

Given an integer N, the task is to find two integers with minimum possible sum such that their product is strictly greater than N.

Examples:



Input: N = 10
Output: 7
Explanation: The integers are 3 and 4. Their product is 3 × 4 = 12, which is greater than N.

Input: N = 1
Output: 3
Explanation: The integers are 1 and 2. Their product is 1 × 2 = 2, which is greater than N.



Naive Approach: Let the required numbers be A and B. The idea is based on the observation that in order to minimize their sum A should be the smallest number greater than √N. Once A is found, B will be equal to the smallest number for which A×B > N, which can be found linearly

Steps to implement-

Code-




// C++ program for the above approach
#include <bits/stdc++.h>
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)
{
    //To store first and second number
    int first,second;
     
    //Finding square root of given number
    float root=sqrt(N);
    int sq_root=(int)root;
     
    //First number will be that integer which
    //is just greater than square root of N
    first=sq_root+1;
     
    //Finding second number
    for(int i=1;i<=N;i++){
        if((first*i)>N){
            second=i;
            break;
        }
    }
     
    //Printing their sum
    cout<<first+second<<endl;
}
 
// Driver Code
int main()
{
    int N = 10;
    // Function Call
    minSum(N);
 
    return 0;
}




// Java program for the above approach
import java.util.*;
 
public 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)
    {
        // To store first and second number
        int first, second = 0;
 
        // Finding square root of given number
        double root = Math.sqrt(N);
        int sq_root = (int)Math.ceil(root);
 
        // First number will be that integer which
        // is just greater than or equal to the square root
        // of N
        first = sq_root;
 
        // Finding second number
        for (int i = 1; i <= N; i++) {
            if ((first * i) > N) {
                second = i;
                break;
            }
        }
 
        // Printing their sum
        System.out.println(first + second);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 10;
        // Function Call
        minSum(N);
    }
}




import math
 
# Function to find the minimum sum of
# two integers such that their product
# is strictly greater than N
def min_sum(N):
    # To store the first and second number
    first, second = 0, 0
     
    # Finding the square root of the given number
    root = math.sqrt(N)
    sq_root = math.ceil(root)
     
    # First number will be that integer which
    # is just greater than the square root of N
    first = sq_root + 1
     
    # Finding the second number
    for i in range(1, N + 1):
        if (first * i) > N:
            second = i
            break
     
    # Printing their sum
    print(first + second)
 
# Driver Code
if __name__ == "__main__":
    N = 10
    # Function Call
    min_sum(N)
 
# This code is contributed by shivamgupta0987654321




using System;
 
class GFG
{
    // Function to find the minimum sum of two integers
    static void MinSum(int N)
    {
        int first, second = 0;
        // Finding the square root of the given number
        double root = Math.Sqrt(N);
        int sqRoot = (int)Math.Ceiling(root);
        first = sqRoot;
        // Finding the second number
        for (int i = 1; i <= N; i++)
        {
            if (first * i > N)
            {
                second = i;
                break;
            }
        }
        // Printing their sum
        Console.WriteLine(first + second);
    }
    // Main method
    static void Main(string[] args)
    {
        int N = 10;
        // Function Call
        MinSum(N);
    }
}




//JavaScript program for the above approach
 
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
function minSum(N)
{
    //To store first and second number
    let first,second;
     
    //Finding square root of given number
    let root=Math.sqrt(N);
    let sq_root=Math.floor(root);
     
    //First number will be that integer which
    //is just greater than square root of N
    first=sq_root+1;
     
    //Finding second number
    for(let i=1;i<=N;i++){
        if((first*i)>N){
            second=i;
            break;
        }
    }
     
    //Printing their sum
    console.log(first+second);
}
 
// Driver Code
    let N = 10;
    // Function Call
    minSum(N);

Output-

7

Time Complexity: O(N)+O(logN)=O(N),O(N) in loop and O(logN) in finding square root
Auxiliary Space: O(1), because no extra space has been used

Efficient Approach: The above solution can be optimized by using Binary Search to find A and B. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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 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# 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




<script>
// Javascript program for the above approach
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
function minSum(N)
{
    // Initialise low as 0 and
    // high as 1e9
    let low = 0, high = 1000000000;
 
    // Iterate to find the first number
    while (low + 1 < high) {
 
        // Find the middle value
        let mid = low + parseInt((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
    let 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
        let mid = low + parseInt((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
    let second = high;
 
    // Print the result
    document.write(first + second);
}
 
// Driver Code
    let N = 10;
 
    // Function Call
    minSum(N);
 
// This code is contributed by rishavmahato348.
</script>

Output
7













Time Complexity: O(log N)
Auxiliary Space: O(1)

Most Efficient Approach: To optimize the above approach, the idea is based on Inequality of Arithmetic and Geometric progression as illustrated below.

From the inequality, If there are two integers A and B, 
(A + B)/2 ≥ √(A×B)
Now, A×B = Product of the two integers, which is N and A+B is sum(=S).
Therefore, S ≥ 2*√N
To get strictly greater product than N, the above equation transforms to: S ≥ 2*√(N+1)

Below is the program for the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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 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# 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




<script>
 
// Javascript program for the above approach
 
// Function to find the minimum sum of
// two integers such that their product
// is strictly greater than N
function minSum(N)
{
    // Store the answer using the
    // AP-GP inequality
    let ans = Math.ceil(2 * Math.sqrt(N + 1));
 
    // Print the answer
    document.write(ans);
}
 
// Driver Code
let N = 10;
 
// Function Call
minSum(N);
 
</script>

Output
7













Time Complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)


Article Tags :