- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are a number N. Our task is to create a program to find the Maximum sum of distinct numbers such that LCM of these numbers is N in C++.

We need to find the sum of all factors of the number N. And add all distinct to find the maximum sum.

**Let’s take an example to understand the problem,**

N = 12

28

All distinct factors of N are 1, 2, 3, 4, 6, 12. Sum = 1 + 2 + 3 + 4 + 6 + 12 = 28

A simple solution will be finding all the factors of the number and then adding all distinct factors to find the result.

For this, we will iterate till the square root of N. And check if the number divides N. If yes, check if it is distinct, if yes add the number and the division quotient Otherwise add the number. Return the final maxSum.

Program to illustrate the working of our solution,

#include <iostream> using namespace std; int calcMaxSumForLCM(int N){ int maxSum = 0; for (int i = 1; i*i <= N; i++){ if (N%i == 0){ if (i == (N/i)) maxSum = maxSum + i; else maxSum = maxSum + i + (N/i); } } return maxSum; } int main(){ int N = 17; cout<<"The sum of distinct numbers such that LCM if these numbers is "<<N<<" is "<<calcMaxSumForLCM(N); return 0; }

The sum of distinct numbers such that LCM if these numbers is 17 is 18

- Related Questions & Answers
- Maximum sum of distinct numbers with LCM as N in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Print n numbers such that their sum is a perfect square
- Find triplet such that number of nodes connecting these triplets is maximum in C++
- Sum of sum of first n natural numbers in C++
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Find LCM of two numbers
- C++ Program to Find the GCD and LCM of n Numbers
- Find three integers less than or equal to N such that their LCM is maximum in C++
- Print N lines of numbers such that every pair among numbers has a GCD K
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++

Advertisements