![]() |
VOOZH | about |
Given a positive integer n, find the maximum sum of distinct positive integers not exceeding n such that the LCM of all the selected integers is exactly n.
Examples:
Input: n = 2
Output: 3
Explanation: The valid numbers whose LCM is 2 are 1 and 2, and their sum is 1 + 2 = 3.Input: n = 5
Output: 6
Explanation: The valid numbers whose LCM is 5 are 1 and 5, and their sum is 1 + 5 = 6.
Table of Content
The idea is to try every possible subset of numbers from 1 to n. For each subset, we compute its LCM and check whether it equals n. If it does, we compute the sum and update the maximum answer.
6
The key idea is to avoid checking all subsets and use a simple property of LCM. For the LCM to be exactly n, every selected number must be a divisor of n, otherwise it will increase the LCM beyond n. Hence, only divisors are valid choices. Since adding more valid numbers increases the sum without changing the LCM, the optimal solution is to include all divisors of n to maximize the sum.
Consider integer: n = 12
Iterate from 1 to √12 ≈ 3
Final Answer:28
28