- 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

# Maximum sum of distinct numbers such that LCM of these numbers is N in C++

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++.

## Problem Description

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,**

## Input

N = 12

## Output

28

## Explanation

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

## Solution Approach

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.

## Example

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; }

## Output

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 Python
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- 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++
- Find LCM of two numbers
- Print N lines of numbers such that every pair among numbers has a GCD K
- Find three integers less than or equal to N such that their LCM is maximum - 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 in C++
- LCM of an array of numbers in Java
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Sum of square of first n odd numbers
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- C++ Program to Find the GCD and LCM of n Numbers

Advertisements