
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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
- The product of two numbers is 1650 and their HCF is 11. Find the LCM of these numbers.
- The product of two numbers is 1530 and their HCF is 15. Find the LCM of these numbers.
- 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.
- Four numbers are inserted between the numbers 4 and 39 such that an AP. results. Find the biggest of these four numbers.
- Find three integers less than or equal to N such that their LCM is maximum in C++
- 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++
- Place N^2 numbers in matrix such that every row has an equal sum in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Find triplet such that number of nodes connecting these triplets is maximum in C++

Advertisements