# Maximum sum of distinct numbers with LCM as N

Given a number N, the task is to find out maximum sum of distinct numbers such that the Least Common Multiple of all these numbers is N.

Input : N = 12 Output : 20 Maximum sum which we can achieve is, 1 + 2 + 3 + 4 + 6 + 12 = 28 Input : N = 15 Output : 24

We can solve this problem by observing some cases, As N needs to be LCM of all numbers, all of them will be divisors of N but because a number can be taken only once in sum, all taken numbers should be distinct. The idea is to ** take every divisor of N once in sum** to maximize the result.

How can we say that the sum we got is maximal sum? The reason is, we have taken all the divisors of N into our sum, now if we take one more number into sum which is not divisor of N, then sum will increase but LCM property will not be hold by all those integers. So it is not possible to add even one more number into our sum, except all divisor of N so our problem boils down to this, given N find sum of all divisors, which can be solved in O(sqrt(N)) time.

So total time complexity of solution will O(sqrt(N)) with O(1) extra space.

Code is given below on above stated concept. Please refer this post for finding all divisors of a number.

## C++

`// C/C++ program to get maximum sum of Numbers ` `// with condition that their LCM should be N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Method returns maximum sum f distinct ` `// number whose LCM is N ` `int` `getMaximumSumWithLCMN(` `int` `N) ` `{ ` ` ` `int` `sum = 0; ` ` ` `int` `LIM = ` `sqrt` `(N); ` ` ` ` ` `// find all divisors which divides 'N' ` ` ` `for` `(` `int` `i = 1; i <= LIM; i++) { ` ` ` `// if 'i' is divisor of 'N' ` ` ` `if` `(N % i == 0) { ` ` ` `// if both divisors are same then add ` ` ` `// it only once else add both ` ` ` `if` `(i == (N / i)) ` ` ` `sum += i; ` ` ` `else` ` ` `sum += (i + N / i); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 12; ` ` ` `cout << getMaximumSumWithLCMN(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to get ` `// maximum sum of Numbers ` `// with condition that ` `// their LCM should be N ` ` ` `class` `GFG { ` ` ` `// Method returns maximum ` ` ` `// sum f distinct number ` ` ` `// whose LCM is N ` ` ` `static` `int` `getMaximumSumWithLCMN(` `int` `N) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `int` `LIM = (` `int` `)Math.sqrt(N); ` ` ` ` ` `// find all divisors which divides 'N' ` ` ` `for` `(` `int` `i = ` `1` `; i <= LIM; i++) { ` ` ` `// if 'i' is divisor of 'N' ` ` ` `if` `(N % i == ` `0` `) { ` ` ` `// if both divisors are same then add ` ` ` `// it only once else add both ` ` ` `if` `(i == (N / i)) ` ` ` `sum += i; ` ` ` `else` ` ` `sum += (i + N / i); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `12` `; ` ` ` `System.out.println(getMaximumSumWithLCMN(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to get ` `# maximum sum of Numbers ` `# with condition that ` `# their LCM should be N ` ` ` `import` `math ` ` ` `# Method returns maximum sum f distinct ` `# number whose LCM is N ` `def` `getMaximumSumWithLCMN(N): ` ` ` ` ` `sum` `=` `0` ` ` `LIM ` `=` `int` `(math.sqrt(N)) ` ` ` ` ` `# find all divisors which divides 'N' ` ` ` `for` `i ` `in` `range` `(` `1` `, LIM ` `+` `1` `): ` ` ` ` ` `# if 'i' is divisor of 'N' ` ` ` `if` `(N ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# if both divisors are same then add ` ` ` `# it only once else add both ` ` ` `if` `(i ` `=` `=` `(N ` `/` `/` `i)): ` ` ` `sum` `=` `sum` `+` `i ` ` ` `else` `: ` ` ` `sum` `=` `sum` `+` `(i ` `+` `N ` `/` `/` `i) ` ` ` ` ` `return` `sum` ` ` `# driver code ` ` ` `N ` `=` `12` `print` `(getMaximumSumWithLCMN(N)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to get maximum sum ` `// of Numbers with condition that ` `// their LCM should be N ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Method returns maximum sum f ` ` ` `// distinct number whose LCM is N ` ` ` `static` `int` `getMaximumSumWithLCMN(` `int` `N) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` `int` `LIM = (` `int` `)Math.Sqrt(N); ` ` ` ` ` `// Find all divisors which divides 'N' ` ` ` `for` `(` `int` `i = 1; i <= LIM; i++) { ` ` ` ` ` `// if 'i' is divisor of 'N' ` ` ` `if` `(N % i == 0) { ` ` ` ` ` `// if both divisors are same then ` ` ` `// add it only once else add both ` ` ` `if` `(i == (N / i)) ` ` ` `sum += i; ` ` ` `else` ` ` `sum += (i + N / i); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 12; ` ` ` `Console.Write(getMaximumSumWithLCMN(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//php program to find the max sum of ` `// numbers whose lcm is n ` ` ` `// Returns maximum sum of numbers with ` `// LCM as N ` `function` `maxSumLCM(` `$n` `) ` `{ ` ` ` `$max_sum` `= 0; ` `// Initialize result ` ` ` ` ` `// Finding a divisor of n and adding ` ` ` `// it to max_sum ` ` ` `for` `(` `$i` `= 1; ` `$i` `* ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$n` `% ` `$i` `== 0) ` ` ` `{ ` ` ` `$max_sum` `+= ` `$i` `; ` ` ` `if` `(` `$n` `/` `$i` `!= ` `$i` `) ` ` ` `$max_sum` `+= (` `$n` `/ ` `$i` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$max_sum` `; ` `} ` ` ` `// Driver code ` `$n` `= 2; ` `echo` `MaxSumLCM(` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

28

This article is contributed by **Utkarsh Trivedi**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Maximum distinct lines passing through a single point
- Exactly n distinct prime factor numbers from a to b
- Numbers having Unique (or Distinct) digits
- Check if all sub-numbers have distinct Digit product
- Find two distinct prime numbers with given product
- Number of distinct prime factors of first n natural numbers
- Largest of two distinct numbers without using any conditional statements or operators
- Split n into maximum composite numbers
- Maximum factors formed by two numbers
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Program to find the maximum difference between the index of any two different numbers
- Maximum and minimum sums from two numbers with digit replacements
- Queries for maximum difference between prime numbers in given ranges