# 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:
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:

1. Run a nested loop from 1 to N.
2. For every number a in the range [1, N], find another integer b such that a != b and (a + b) divides a * b.
3. If the condition is satisfied, the value of (a + b) is stored in a variable to keep the track of the maximum value obtained.
4. Finally, the maximum value of (a + b) is returned.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the largest value``// of a + b satisfying the given condition``#include ``using` `namespace` `std;` `// Function to return the maximum sum of``// a + b satisfying the given condition``int` `getLargestSum(``int` `N)``{``    ``// Initialize max_sum``    ``int` `max_sum = 0;` `    ``// Consider all the possible pairs``    ``for` `(``int` `i = 1; i <= N; i++) {``        ``for` `(``int` `j = i + 1; j <= N; j++) {` `            ``// Check if the product is``            ``// divisible by the sum``            ``if` `(i * j % (i + j) == 0)` `                ``// Storing the maximum sum``                ``// in the max_sum variable``                ``max_sum = max(max_sum, i + j);``        ``}``    ``}` `    ``// Return the max_sum value``    ``return` `max_sum;``}` `// Driver code``int` `main()``{``    ``int` `N = 25;` `    ``int` `max_sum = getLargestSum(N);` `    ``cout << max_sum << endl;``    ``return` `0;``}`

## Java

 `// Java implementation to find the largest value``// of a + b satisfying the given condition``import` `java.util.*;`` ` `class` `GFG{``  ` `// Function to return the maximum sum of``// a + b satisfying the given condition``static` `int` `getLargestSum(``int` `N)``{``    ``// Initialize max_sum``    ``int` `max_sum = ``0``;` `    ``// Consider all the possible pairs``    ``for` `(``int` `i = ``1``; i <= N; i++) {``        ``for` `(``int` `j = i + ``1``; j <= N; j++) {` `            ``// Check if the product is``            ``// divisible by the sum``            ``if` `(i * j % (i + j) == ``0``)` `                ``// Storing the maximum sum``                ``// in the max_sum variable``                ``max_sum = Math.max(max_sum, i + j);``        ``}``    ``}` `    ``// Return the max_sum value``    ``return` `max_sum;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``25``;` `    ``int` `max_sum = getLargestSum(N);` `    ``System.out.print(max_sum );``}``}` `// This code is contributed by shivanisinghss2110`

## Python3

 `# Python3 implementation to find the largest value``# of a + b satisfying the given condition` `# Function to return the maximum sum of``# a + b satisfying the given condition``def` `getLargestSum(N):``    ``# Initialize max_sum``    ``max_sum ``=` `0` `    ``# Consider all the possible pairs``    ``for` `i ``in` `range``(``1``,N``+``1``):``        ``for` `j ``in` `range``(i ``+` `1``, N ``+` `1``, ``1``):` `            ``# Check if the product is``            ``# divisible by the sum``            ``if` `(i ``*` `j ``%` `(i ``+` `j) ``=``=` `0``):` `                ``# Storing the maximum sum``                ``# in the max_sum variable``                ``max_sum ``=` `max``(max_sum, i ``+` `j)` `    ``# Return the max_sum value``    ``return` `max_sum` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``N ``=` `25` `    ``max_sum ``=` `getLargestSum(N)``    ``print``(max_sum)` `# This code is contributed by Surendra_Gangwar`

## C#

 `// C# implementation to find the largest value``// of a + b satisfying the given condition``using` `System;` `class` `GFG{``    ` `    ``// Function to return the maximum sum of``    ``// a + b satisfying the given condition``    ``static` `int` `getLargestSum(``int` `N)``    ``{``        ``// Initialize max_sum``        ``int` `max_sum = 0;``    ` `        ``// Consider all the possible pairs``        ``for` `(``int` `i = 1; i <= N; i++) {``            ``for` `(``int` `j = i + 1; j <= N; j++) {``    ` `                ``// Check if the product is``                ``// divisible by the sum``                ``if` `(i * j % (i + j) == 0)``    ` `                    ``// Storing the maximum sum``                    ``// in the max_sum variable``                    ``max_sum = Math.Max(max_sum, i + j);``            ``}``        ``}``    ` `        ``// Return the max_sum value``        ``return` `max_sum;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `N = 25;``    ` `        ``int` `max_sum = getLargestSum(N);``    ` `        ``Console.WriteLine(max_sum );``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``
Output:
`36`

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 &leq; N and (x + y) * x &leq; N.

6. Therefore, all the factors are checked from 1 to N0.5.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to  find the largest value``// of a + b satisfying the given condition``#include ``using` `namespace` `std;` `// Function to return the maximum sum of``// a + b satisfying the given condition``int` `getLargestSum(``int` `N)``{``    ``int` `max_sum = 0; ``// Initialize max_sum` `    ``// Consider all possible pairs and check``    ``// the sum divides product property``    ``for` `(``int` `i = 1; i * i <= N; i++) {``        ``for` `(``int` `j = i + 1; j * j <= N; j++) {` `            ``// To find the largest factor k``            ``int` `k = N / j;``            ``int` `a = k * i;``            ``int` `b = k * j;` `            ``// Check if the product is``            ``// divisible by the sum``            ``if` `(a <= N && b <= N``                ``&& a * b % (a + b) == 0)` `                ``// Storing the maximum sum``                ``// in the max_sum variable``                ``max_sum = max(max_sum, a + b);``        ``}``    ``}` `    ``// Return the max_sum value``    ``return` `max_sum;``}` `// Driver code``int` `main()``{``    ``int` `N = 25;``    ``int` `max_sum = getLargestSum(N);` `    ``cout << max_sum << endl;``    ``return` `0;``}`

## Java

 `// Java implementation to find the largest value``// of a + b satisfying the given condition` `class` `GFG{` `// Function to return the maximum sum of``// a + b satisfying the given condition``static` `int` `getLargestSum(``int` `N)``{``    ``// Initialize max_sum``    ``int` `max_sum = ``0``;` `    ``// Consider all possible pairs and check``    ``// the sum divides product property``    ``for` `(``int` `i = ``1``; i * i <= N; i++) {``         ``for` `(``int` `j = i + ``1``; j * j <= N; j++) {` `              ``// To find the largest factor k``              ``int` `k = N / j;``              ``int` `a = k * i;``              ``int` `b = k * j;` `               ``// Check if the product is``               ``// divisible by the sum``               ``if` `(a <= N && b <= N &&``                   ``a * b % (a + b) == ``0``)` `                   ``// Storing the maximum sum``                   ``// in the max_sum variable``                   ``max_sum = Math.max(max_sum, a + b);``        ``}``    ``}` `    ``// Return the max_sum value``    ``return` `max_sum;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``25``;``    ``int` `max_sum = getLargestSum(N);``    ``System.out.print(max_sum + ``"\n"``);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation to find the largest value``# of a + b satisfying the given condition` `# Function to return the maximum sum of``# a + b satisfying the given condition``def` `getLargestSum(N) :` `    ``max_sum ``=` `0``; ``# Initialize max_sum` `    ``# Consider all possible pairs and check``    ``# the sum divides product property``    ``for` `i ``in` `range``(``1``, ``int``(N ``*``*` `(``1``/``2``))``+``1``) :``        ``for` `j ``in` `range``(i ``+` `1``, ``int``(N ``*``*` `(``1``/``2``)) ``+` `1``) :``            ` `            ``# To find the largest factor k``            ``k ``=` `N ``/``/` `j;``            ``a ``=` `k ``*` `i;``            ``b ``=` `k ``*` `j;``            ` `            ``# Check if the product is``            ``# divisible by the sum``            ``if` `(a <``=` `N ``and` `b <``=` `N ``and` `a ``*` `b ``%` `(a ``+` `b) ``=``=` `0``) :``                ``# Storing the maximum sum``                ``# in the max_sum variable``                ``max_sum ``=` `max``(max_sum, a ``+` `b);``                ` `    ``# Return the max_sum value``    ``return` `max_sum;``    ` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:``    ``N ``=` `25``;``    ``max_sum ``=` `getLargestSum(N);``    ``print``(max_sum);` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation to find the largest value``// of a + b satisfying the given condition``using` `System;` `class` `GFG{` `// Function to return the maximum sum of``// a + b satisfying the given condition``static` `int` `getLargestSum(``int` `N)``{``    ` `    ``// Initialize max_sum``    ``int` `max_sum = 0;` `    ``// Consider all possible pairs and check``    ``// the sum divides product property``    ``for``(``int` `i = 1; i * i <= N; i++)``    ``{``       ``for``(``int` `j = i + 1; j * j <= N; j++)``       ``{``          ``// To find the largest factor k``          ``int` `k = N / j;``          ``int` `a = k * i;``          ``int` `b = k * j;``          ` `          ``// Check if the product is``          ``// divisible by the sum``          ``if` `(a <= N && b <= N &&``              ``a * b % (a + b) == 0)``              ` `              ``// Storing the maximum sum``              ``// in the max_sum variable``              ``max_sum = Math.Max(max_sum, a + b);``        ``}``    ``}` `    ``// Return the max_sum value``    ``return` `max_sum;``}` `// Driver code``static` `public` `void` `Main(String[] args)``{``    ``int` `N = 25;``    ``int` `max_sum = getLargestSum(N);``    ` `    ``Console.Write(max_sum + ``"\n"``);``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``
Output:
`36`

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)

My Personal Notes arrow_drop_up