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:
- 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++
// 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; } |
Python3
# Python 3 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 |
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:
- 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
- Now, upon dividing the above values:
(a * b) /(a + b) = k * ((x * y)/(x + y)).
- 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.
- Therefore, k is the largest factor possible for which (k * x) and (k * y) remain less than N.
- 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.
- Therefore, all the factors are checked from 1 to N0.5.
Below is the implementation of the above approach:
// 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; } |
36
Time Complexity: Though there are two loops, each loop runs for at most sqrt(N) time. Therefore, the overall time complexity O(N).
Recommended Posts:
- Count of pairs satisfying the given condition
- Largest sub-set possible for an array satisfying the given condition
- Check if elements of an array can be arranged satisfying the given condition
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Partition the digits of an integer such that it satisfies a given condition
- Find an integer that is common in the maximum number of given arithmetic progressions
- Find x and y satisfying ax + by = n
- Replace all ‘0’ with ‘5’ in an input Integer
- Compute the maximum power with a given condition
- Maximum size of sub-array that satisfies the given condition
- Find the final X and Y when they are Altering under given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Find permutation of first N natural numbers that satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
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.
Improved By : SURENDRA_GANGWAR

