Minimum sum of two integers whose product is strictly greater than N
Last Updated :
07 Nov, 2023
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-
- Declare two variables first and second to store the answer
- Find the square root of the given number
- The first number will be that integer which is just greater than the square root
- Find the second number by running a loop
- In the last print the sum of the first and the second number
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void minSum( int N)
{
int first,second;
float root= sqrt (N);
int sq_root=( int )root;
first=sq_root+1;
for ( int i=1;i<=N;i++){
if ((first*i)>N){
second=i;
break ;
}
}
cout<<first+second<<endl;
}
int main()
{
int N = 10;
minSum(N);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static void minSum( int N)
{
int first, second = 0 ;
double root = Math.sqrt(N);
int sq_root = ( int )Math.ceil(root);
first = sq_root;
for ( int i = 1 ; i <= N; i++) {
if ((first * i) > N) {
second = i;
break ;
}
}
System.out.println(first + second);
}
public static void main(String[] args)
{
int N = 10 ;
minSum(N);
}
}
|
Python3
import math
def min_sum(N):
first, second = 0 , 0
root = math.sqrt(N)
sq_root = math.ceil(root)
first = sq_root + 1
for i in range ( 1 , N + 1 ):
if (first * i) > N:
second = i
break
print (first + second)
if __name__ = = "__main__" :
N = 10
min_sum(N)
|
C#
using System;
class GFG
{
static void MinSum( int N)
{
int first, second = 0;
double root = Math.Sqrt(N);
int sqRoot = ( int )Math.Ceiling(root);
first = sqRoot;
for ( int i = 1; i <= N; i++)
{
if (first * i > N)
{
second = i;
break ;
}
}
Console.WriteLine(first + second);
}
static void Main( string [] args)
{
int N = 10;
MinSum(N);
}
}
|
Javascript
function minSum(N)
{
let first,second;
let root=Math.sqrt(N);
let sq_root=Math.floor(root);
first=sq_root+1;
for (let i=1;i<=N;i++){
if ((first*i)>N){
second=i;
break ;
}
}
console.log(first+second);
}
let N = 10;
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:
- Initialize two variables low = 0 and high = 109.
- Iterate until (high – low) is greater than 1 and do the following:
- Find the value of middle-range mid as (low + high)/2.
- Now, compare √N with the middle element mid, and if √N is less than or equal to the middle element, then high as mid.
- Else, update low as mid.
- After all the above steps set A = high.
- Repeat the same procedure to find B such that A×B > N.
- After the above steps, print the sum of A and B as the result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
void minSum( int N)
{
ll low = 0, high = 1e9;
while (low + 1 < high) {
ll mid = low + (high - low) / 2;
if (mid * mid >= N) {
high = mid;
}
else {
low = mid;
}
}
ll first = high;
low = 0;
high = 1e9;
while (low + 1 < high) {
ll mid = low + (high - low) / 2;
if (first * mid > N) {
high = mid;
}
else {
low = mid;
}
}
ll second = high;
cout << first + second;
}
int main()
{
int N = 10;
minSum(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void minSum( int N)
{
long low = 0 , high = 1000000000 ;
while (low + 1 < high)
{
long mid = low + (high - low) / 2 ;
if (mid * mid >= N)
{
high = mid;
}
else
{
low = mid;
}
}
long first = high;
low = 0 ;
high = 1000000000 ;
while (low + 1 < high)
{
long mid = low + (high - low) / 2 ;
if (first * mid > N)
{
high = mid;
}
else
{
low = mid;
}
}
long second = high;
System.out.println(first + second);
}
public static void main(String[] args)
{
int N = 10 ;
minSum(N);
}
}
|
Python3
def minSum(N):
low = 0
high = 1000000000
while (low + 1 < high):
mid = low + (high - low) / 2
if (mid * mid > = N):
high = mid
else :
low = mid
first = high
low = 0
high = 1000000000
while (low + 1 < high):
mid = low + (high - low) / 2
if (first * mid > N):
high = mid
else :
low = mid
second = high
print ( round (first + second))
N = 10
minSum(N)
|
C#
using System;
class GFG{
static void minSum( int N)
{
long low = 0, high = 1000000000;
while (low + 1 < high)
{
long mid = low + (high - low) / 2;
if (mid * mid >= N)
{
high = mid;
}
else
{
low = mid;
}
}
long first = high;
low = 0;
high = 1000000000;
while (low + 1 < high)
{
long mid = low + (high - low) / 2;
if (first * mid > N)
{
high = mid;
}
else
{
low = mid;
}
}
long second = high;
Console.WriteLine( first + second);
}
static public void Main()
{
int N = 10;
minSum(N);
}
}
|
Javascript
<script>
function minSum(N)
{
let low = 0, high = 1000000000;
while (low + 1 < high) {
let mid = low + parseInt((high - low) / 2);
if (mid * mid >= N) {
high = mid;
}
else {
low = mid;
}
}
let first = high;
low = 0;
high = 1000000000;
while (low + 1 < high) {
let mid = low + parseInt((high - low) / 2);
if (first * mid > N) {
high = mid;
}
else {
low = mid;
}
}
let second = high;
document.write(first + second);
}
let N = 10;
minSum(N);
</script>
|
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++14
#include <bits/stdc++.h>
using namespace std;
void minSum( int N)
{
int ans = ceil (2 * sqrt (N + 1));
cout << ans;
}
int main()
{
int N = 10;
minSum(N);
return 0;
}
|
Java
import java.lang.*;
class GFG{
static void minSum( int N)
{
int ans = ( int )Math.ceil( 2 * Math.sqrt(N + 1 ));
System.out.println( ans);
}
public static void main(String[] args)
{
int N = 10 ;
minSum(N);
}
}
|
Python3
import math
def minSum(N):
ans = math.ceil( 2 * math.sqrt(N + 1 ))
print (math.trunc(ans))
N = 10
minSum(N)
|
C#
using System;
class GFG{
static void minSum( int N)
{
int ans = ( int )Math.Ceiling(2 * Math.Sqrt(N + 1));
Console.WriteLine( ans);
}
static public void Main()
{
int N = 10;
minSum(N);
}
}
|
Javascript
<script>
function minSum(N)
{
let ans = Math.ceil(2 * Math.sqrt(N + 1));
document.write(ans);
}
let N = 10;
minSum(N);
</script>
|
Time Complexity: O(logN) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...