Find the maximum sum (a+b) for a given input integer N satisfying the given condition

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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the largest value
// of a + b satisfying the given condition
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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).

Efficient Approach: One observation which can be made is that if two numbers a and b exists 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to  find the largest value
// of a + b satisfying the given condition
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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).

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.