Maximum sum of distinct numbers with LCM as N
Last Updated :
09 Dec, 2022
Given a number N, the task is to find out the maximum sum of distinct numbers such that the Least Common Multiple of all these numbers is N.
Example:
Input : N = 12
Output : 28
Maximum sum which we can achieve is,
1 + 2 + 3 + 4 + 6 + 12 = 28
Input : N = 15
Output : 24
We can solve this problem by observing some cases, As N needs to be LCM of all numbers, all of them will be divisors of N but because a number can be taken only once in sum, all taken numbers should be distinct. The idea is to take every divisor of N once in sum to maximize the result.
How can we say that the sum we got is maximal sum? The reason is, we have taken all the divisors of N into our sum, now if we take one more number into sum which is not divisor of N, then sum will increase but LCM property will not be held by all those integers. So it is not possible to add even one more number into our sum, except all divisor of N so our problem boils down to this, given N find sum of all divisors, which can be solved in O(sqrt(N)) time.
So total time complexity of solution will O(sqrt(N)) with O(1) extra space.
Code is given below on above-stated concept. Please refer this post for finding all divisors of a number.
C++
#include <bits/stdc++.h>
using namespace std;
int getMaximumSumWithLCMN( int N)
{
int sum = 0;
int LIM = sqrt (N);
for ( int i = 1; i <= LIM; i++) {
if (N % i == 0) {
if (i == (N / i))
sum += i;
else
sum += (i + N / i);
}
}
return sum;
}
int main()
{
int N = 12;
cout << getMaximumSumWithLCMN(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int getMaximumSumWithLCMN( int N)
{
int sum = 0 ;
int LIM = ( int )Math.sqrt(N);
for ( int i = 1 ; i <= LIM; i++) {
if (N % i == 0 ) {
if (i == (N / i))
sum += i;
else
sum += (i + N / i);
}
}
return sum;
}
public static void main(String[] args)
{
int N = 12 ;
System.out.println(getMaximumSumWithLCMN(N));
}
}
|
Python3
import math
def getMaximumSumWithLCMN(N):
sum = 0
LIM = int (math.sqrt(N))
for i in range ( 1 , LIM + 1 ):
if (N % i = = 0 ):
if (i = = (N / / i)):
sum = sum + i
else :
sum = sum + (i + N / / i)
return sum
N = 12
print (getMaximumSumWithLCMN(N))
|
C#
using System;
class GFG {
static int getMaximumSumWithLCMN( int N)
{
int sum = 0;
int LIM = ( int )Math.Sqrt(N);
for ( int i = 1; i <= LIM; i++) {
if (N % i == 0) {
if (i == (N / i))
sum += i;
else
sum += (i + N / i);
}
}
return sum;
}
public static void Main()
{
int N = 12;
Console.Write(getMaximumSumWithLCMN(N));
}
}
|
PHP
<?php
function maxSumLCM( $n )
{
$max_sum = 0;
for ( $i = 1; $i * $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
$max_sum += $i ;
if ( $n / $i != $i )
$max_sum += ( $n / $i );
}
}
return $max_sum ;
}
$n = 2;
echo MaxSumLCM( $n ), "\n" ;
?>
|
Javascript
<script>
function getMaximumSumWithLCMN(N)
{
let sum = 0;
let LIM = Math.sqrt(N);
for (let i = 1; i <= LIM; i++)
{
if (N % i == 0)
{
if (i == (N / i))
sum += i;
else
sum += (i + N / i);
}
}
return sum;
}
let N = 12;
document.write(getMaximumSumWithLCMN(N) + "<br>" );
</script>
|
Time Complexity: O(?n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...