Find the maximum sum (a+b) for a given input integer N satisfying the given condition
Last Updated :
22 Nov, 2021
Given an integer N, the task is to find the largest sum (a + b) {1 ? a ? N, 1 ? b ? N} such that a * b/(a + b) is an integer (i.e a + b divides a * b) and a != b.
Examples:
Input: N = 10
Output: 9
Explanation: The numbers a = 3 and b = 6 leads to sum = 9 also 6 * 3 = 18 which is divisible by 6 + 3 = 9
Input: N = 20
Output: 27
Naive Approach: The idea to solve this problem with the naive approach is to use the concept of nested loops. The following steps can be followed to compute the result:
- Run a nested loop from 1 to N.
- For every number a in the range [1, N], find another integer b such that a != b and (a + b) divides a * b.
- If the condition is satisfied, the value of (a + b) is stored in a variable to keep the track of the maximum value obtained.
- Finally, the maximum value of (a + b) is returned.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getLargestSum( int N)
{
int max_sum = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
if (i * j % (i + j) == 0)
max_sum = max(max_sum, i + j);
}
}
return max_sum;
}
int main()
{
int N = 25;
int max_sum = getLargestSum(N);
cout << max_sum << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int getLargestSum( int N)
{
int max_sum = 0 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = i + 1 ; j <= N; j++) {
if (i * j % (i + j) == 0 )
max_sum = Math.max(max_sum, i + j);
}
}
return max_sum;
}
public static void main(String[] args)
{
int N = 25 ;
int max_sum = getLargestSum(N);
System.out.print(max_sum );
}
}
|
Python3
def getLargestSum(N):
max_sum = 0
for i in range ( 1 ,N + 1 ):
for j in range (i + 1 , N + 1 , 1 ):
if (i * j % (i + j) = = 0 ):
max_sum = max (max_sum, i + j)
return max_sum
if __name__ = = '__main__' :
N = 25
max_sum = getLargestSum(N)
print (max_sum)
|
C#
using System;
class GFG{
static int getLargestSum( int N)
{
int max_sum = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
if (i * j % (i + j) == 0)
max_sum = Math.Max(max_sum, i + j);
}
}
return max_sum;
}
public static void Main( string [] args)
{
int N = 25;
int max_sum = getLargestSum(N);
Console.WriteLine(max_sum );
}
}
|
Javascript
<script>
function getLargestSum(N) {
var max_sum = 0;
for (i = 1; i <= N; i++) {
for (j = i + 1; j <= N; j++) {
if (i * j % (i + j) == 0)
max_sum = Math.max(max_sum, i + j);
}
}
return max_sum;
}
var N = 25;
var max_sum = getLargestSum(N);
document.write(max_sum);
</script>
|
Time Complexity: Since the nested for loop runs (N * (N + 1)) / 2 times, the time complexity of the above solution is O(N2).
Auxiliary Space: O(1)
Efficient Approach: One observation that can be made is that if two numbers a and b exist such that their product is divisible by their sum then they are not relatively prime, i.e, their GCD is not one. This can be proved using Euclidean Algorithm.
Therefore, by using the above observation, the following steps can be followed to compute the result:
1. If a can be expressed as k * x and b can be expressed as k * y such that x and y are coprimes, then:
a + b = k(x + y)
a * b = k2
2. Now, upon dividing the above values:
(a * b) /(a + b) = k * ((x * y)/(x + y)).
3. Since it is known that x and y are coprime, (x * y) will not be divisible by (x + y). This means k must be divisible by x + y.
4. Therefore, k is the largest factor possible for which (k * x) and (k * y) remain less than N.
5. Clearly, the minimum value of k for which it is divisible by (x + y) is (x + y). This means that (x + y) * y ≤ N and (x + y) * x ≤ N.
6. Therefore, all the factors are checked from 1 to N0.5.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getLargestSum( int N)
{
int max_sum = 0;
for ( int i = 1; i * i <= N; i++) {
for ( int j = i + 1; j * j <= N; j++) {
int k = N / j;
int a = k * i;
int b = k * j;
if (a <= N && b <= N
&& a * b % (a + b) == 0)
max_sum = max(max_sum, a + b);
}
}
return max_sum;
}
int main()
{
int N = 25;
int max_sum = getLargestSum(N);
cout << max_sum << endl;
return 0;
}
|
Java
class GFG{
static int getLargestSum( int N)
{
int max_sum = 0 ;
for ( int i = 1 ; i * i <= N; i++) {
for ( int j = i + 1 ; j * j <= N; j++) {
int k = N / j;
int a = k * i;
int b = k * j;
if (a <= N && b <= N &&
a * b % (a + b) == 0 )
max_sum = Math.max(max_sum, a + b);
}
}
return max_sum;
}
public static void main(String[] args)
{
int N = 25 ;
int max_sum = getLargestSum(N);
System.out.print(max_sum + "\n" );
}
}
|
Python3
def getLargestSum(N) :
max_sum = 0 ;
for i in range ( 1 , int (N * * ( 1 / 2 )) + 1 ) :
for j in range (i + 1 , int (N * * ( 1 / 2 )) + 1 ) :
k = N / / j;
a = k * i;
b = k * j;
if (a < = N and b < = N and a * b % (a + b) = = 0 ) :
max_sum = max (max_sum, a + b);
return max_sum;
if __name__ = = "__main__" :
N = 25 ;
max_sum = getLargestSum(N);
print (max_sum);
|
C#
using System;
class GFG{
static int getLargestSum( int N)
{
int max_sum = 0;
for ( int i = 1; i * i <= N; i++)
{
for ( int j = i + 1; j * j <= N; j++)
{
int k = N / j;
int a = k * i;
int b = k * j;
if (a <= N && b <= N &&
a * b % (a + b) == 0)
max_sum = Math.Max(max_sum, a + b);
}
}
return max_sum;
}
static public void Main(String[] args)
{
int N = 25;
int max_sum = getLargestSum(N);
Console.Write(max_sum + "\n" );
}
}
|
Javascript
<script>
function getLargestSum(N)
{
let max_sum = 0;
for (let i = 1; i * i <= N; i++)
{
for (let j = i + 1; j * j <= N; j++)
{
let k = parseInt(N / j, 10);
let a = k * i;
let b = k * j;
if (a <= N && b <= N &&
a * b % (a + b) == 0)
max_sum = Math.max(max_sum, a + b);
}
}
return max_sum;
}
let N = 25;
let max_sum = getLargestSum(N);
document.write(max_sum + "</br>" );
</script>
|
Time Complexity: Though there are two loops, each loop runs for at most sqrt(N) time. Therefore, the overall time complexity O(N).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...