- 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 given a number N. Our task is to create a program to find the maximum sum of distinct numbers with LCM as N in C++.

Here, we need to find the sum of maximum numbers that have N as the Lowest Common Multiple (LCM).

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

N = 10

18

Maximum sum with LCM 10 is 1 + 2 + 5 + 10 = 18

A simple solution to the problem would be using the idea that if we want the number N as LCM, then we need to take all the distinct divisors of N. And add them up to get the maxSum.

For this we will find all the factors of N. And then add them up, this will be given the maximum as we have considered all the numbers that can contribute to the LCM being N.

Program to illustrate the working of our solution,

#include <iostream> using namespace std; int calcFactorSum(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 = 42; cout<<"The sum of distinct numbers with LCM as "<<N<<" is "<<calcFactorSum(N); return 0; }

The sum of distinct numbers with LCM as 42 is 96

- Related Questions & Answers
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Find two numbers with sum and product both same as N in C++
- C++ Program to Find the GCD and LCM of n Numbers
- Sum of sum of first n natural numbers in C++
- Find two numbers with sum and product both same as N in C++ Program
- C++ program to find two numbers with sum and product both same as N
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- C++ find four factors of N with maximum product and sum equal to N .
- Maximum circular subarray sum in C++\n
- Find four factors of N with maximum product and sum equal to N in C++
- Find LCM of two numbers
- Print all possible sums of consecutive numbers with sum N in C++
- Sum of first n natural numbers in C Program
- Maximum length subarray with LCM equal to product in C++
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++

Advertisements