The Wayback Machine - https://web.archive.org/web/20240818032033/https://www.geeksforgeeks.org/sum-of-lcm-1-n-lcm-2-n-lcm-3-n-lcm-n-n/
Open In App

Sum of LCM(1, n), LCM(2, n), LCM(3, n), … , LCM(n, n)

Last Updated : 21 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an integer n, the task is to find the sum: 

LCM(1, n) + LCM(2, n) + LCM(3, n) + … + LCM(n, n) 
where LCM(i, n) is the Least Common Multiple of i and n. 

Examples:  

Input: 3 
Output: 10 
LCM(1, 3) + LCM(2, 3) + LCM(3, 3) = 3 + 6 + 3 = 12

Input: 5 
Output: 55 
LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5) = 55 

Naive Approach: LCM of two numbers a and b = (a * b) / gcd(a, b) where gcd(a, b) is the Greatest Common Divisor of a and b.  

  • Calculate the values of individual LCM for all pairs starting from (1, n) to (n, n).
  • Sum all the LCM results from the previous step.
  • Print the sum in the end.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to calculate the required LCM sum
ll lcmSum(long long n)
{
    ll sum = 0;
 
    for (long long int i = 1; i <= n; i++) {
 
        // GCD of i and n
        long long int gcd = __gcd(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        long long int lcm = (i * n) / gcd;
 
        // Update sum
        sum = sum + lcm;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << lcmSum(n);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// return gcd of two numbers
static int gcd(int a,int b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
 
}
 
// Function to calculate the required LCM sum
static int lcmSum(int n)
{
    int sum = 0;
 
    for (int i = 1; i <= n; i++)
    {
 
        // GCD of i and n
        int gcd = gcd(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        int lcm = (i * n) / gcd;
 
        // Update sum
        sum = sum + lcm;
    }
 
    return sum;
}
 
// Driver code
public static void main(String args[])
{
    int n = 3;
 
    System.out.println(lcmSum(n));
}
}
 
// This code is contributed by
// Surendra _Gangwar


Python3




# Python3 implementation of the approach
import math
 
# Function to calculate the required LCM sum
def lcmSum(n):
 
    Sum = 0
    for i in range(1, n + 1):
 
        # GCD of i and n
        gcd = math.gcd(i, n)
 
        # LCM of i and n i.e. (i * n) / gcd(i, n)
        lcm = (i * n) // gcd
 
        # Update sum
        Sum = Sum + lcm
 
    return Sum
 
# Driver code
if __name__ == "__main__":
 
    n = 3
    print(lcmSum(n))
 
# This code is contributed by Rituraj Jain


C#




// C# implementation of the approach
class GFG
{
 
// return gcd of two numbers
static int gcd1(int a,int b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd1(a - b, b);
    return gcd1(a, b - a);
 
}
 
// Function to calculate the required LCM sum
static int lcmSum(int n)
{
    int sum = 0;
 
    for (int i = 1; i <= n; i++)
    {
 
        // GCD of i and n
        int gcd = gcd1(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        int lcm = (i * n) / gcd;
 
        // Update sum
        sum = sum + lcm;
    }
 
    return sum;
}
 
// Driver code
static void Main()
{
    int n = 3;
 
    System.Console.WriteLine(lcmSum(n));
}
}
 
// This code is contributed by chandan_jnu


PHP




<?php
// PHP implementation of the approach
 
function __gcd($a, $b)
{
    if($b == 0)
    return $a;
    return __gcd($b, $a % $b);
}
 
// Function to calculate the required LCM sum
function lcmSum($n)
{
    $sum = 0;
 
    for ($i = 1; $i <= $n; $i++)
    {
 
        // GCD of i and n
        $gcd = __gcd($i, $n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        $lcm = ($i * $n) / $gcd;
 
        // Update sum
        $sum = $sum + $lcm;
    }
 
    return $sum;
}
 
// Driver code
$n = 3;
 
echo lcmSum($n);
 
// This code is contributed by chandan_jnu
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// return gcd of two numbers
function gcd(a, b)
{
     
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd(a - b, b);
         
    return gcd(a, b - a);
}
 
// Function to calculate the required LCM sum
function lcmSum(n)
{
    var sum = 0;
 
    for(var i = 1; i <= n; i++)
    {
 
        // GCD of i and n
        var _gcd = gcd(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        var lcm = (i * n) / _gcd;
 
        // Update sum
        sum = sum + lcm;
    }
    return sum;
}
 
// Driver code
var n = 3;
 
document.write(lcmSum(n));
     
// This code is contributed by Ankita saini
    
</script>


Output: 

12

 

Time Complexity: O(n * logn), where n represents the given integer.

Auxiliary Space: O(1), no extra space is required, so it is a constant.
 

Efficient Approach: Using Euler Totient Function, 
?LCM(i, n) = ((?(d * ETF(d)) + 1) * n) / 2 
where ETF(d) is Euler totient function of d and d belongs to the set of divisors of n.

Example: 

Let n be 5 then LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5) 
= 5 + 10 + 15 + 20 + 5 
= 55
With Euler Totient Function: 
All divisors of 5 are {1, 5} 
Hence, ((1*ETF(1) + 5*ETF(5) + 1) * 5) / 2 = 55 

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
#define n 1000002
#define ll long long int
 
ll phi[n + 2], ans[n + 2];
 
// Euler totient Function
void ETF()
{
    for (int i = 1; i <= n; i++) {
        phi[i] = i;
    }
 
    for (int i = 2; i <= n; i++) {
        if (phi[i] == i) {
            phi[i] = i - 1;
            for (int j = 2 * i; j <= n; j += i) {
                phi[j] = (phi[j] * (i - 1)) / i;
            }
        }
    }
}
 
// Function to return the required LCM sum
ll LcmSum(int m)
{
    ETF();
 
    for (int i = 1; i <= n; i++) {
 
        // Summation of d * ETF(d) where
        // d belongs to set of divisors of n
        for (int j = i; j <= n; j += i) {
            ans[j] += (i * phi[i]);
        }
    }
 
    ll answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
int main()
{
    int m = 5;
 
    cout << LcmSum(m);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
     
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
static void ETF()
{
    for (int i = 1; i <= n; i++)
    {
        phi[i] = i;
    }
 
    for (int i = 2; i <= n; i++)
    {
        if (phi[i] == i)
        {
            phi[i] = i - 1;
            for (int j = 2 * i; j <= n; j += i)
            {
                phi[j] = (phi[j] * (i - 1)) / i;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int LcmSum(int m)
{
    ETF();
 
    for (int i = 1; i <= n; i++)
    {
 
        // Summation of d * ETF(d) where
        // d belongs to set of divisors of n
        for (int j = i; j <= n; j += i)
        {
            ans[j] += (i * phi[i]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
public static void main (String[] args)
{
    int m = 5;
    System.out.println(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu


Python3




# Python3 implementation of the approach
n = 100002;
 
phi = [0] * (n + 2);
ans = [0] * (n + 2);
 
# Euler totient Function
def ETF():
 
    for i in range(1, n + 1):
        phi[i] = i;
 
    for i in range(2, n + 1):
        if (phi[i] == i):
            phi[i] = i - 1;
            for j in range(2 * i, n + 1, i):
                phi[j] = (phi[j] * (i - 1)) // i;
 
# Function to return the required LCM sum
def LcmSum(m):
    ETF();
 
    for i in range(1, n + 1):
 
        # Summation of d * ETF(d) where
        # d belongs to set of divisors of n
        for j in range(i, n + 1, i):
            ans[j] += (i * phi[i]);
 
    answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer // 2;
    return answer;
 
# Driver code
m = 5;
print(LcmSum(m));
 
# This code is contributed
# by chandan_jnu


C#




// C# implementation of the approach
using System;
 
class GFG
{
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
static void ETF()
{
    for (int i = 1; i <= n; i++)
    {
        phi[i] = i;
    }
 
    for (int i = 2; i <= n; i++)
    {
        if (phi[i] == i)
        {
            phi[i] = i - 1;
            for (int j = 2 * i; j <= n; j += i)
            {
                phi[j] = (phi[j] * (i - 1)) / i;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int LcmSum(int m)
{
    ETF();
 
    for (int i = 1; i <= n; i++)
    {
 
        // Summation of d * ETF(d) where
        // d belongs to set of divisors of n
        for (int j = i; j <= n; j += i)
        {
            ans[j] += (i * phi[i]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
static void Main()
{
    int m = 5;
    Console.WriteLine(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu


PHP




<?php
// PHP implementation of the approach
$n = 10002;
 
$phi = array_fill(0, $n + 2, 0);
$ans = array_fill(0, $n + 2, 0);
 
// Euler totient Function
function ETF()
{
    global $phi, $n;
    for ($i = 1; $i <= $n; $i++)
    {
        $phi[$i] = $i;
    }
 
    for ($i = 2; $i <= $n; $i++)
    {
        if ($phi[$i] == $i)
        {
            $phi[$i] = $i - 1;
            for ($j = 2 * $i; $j <= $n; $j += $i)
            {
                $phi[$j] = (int)(($phi[$j] *
                                 ($i - 1)) / $i);
            }
        }
    }
}
 
// Function to return the required LCM sum
function LcmSum($m)
{
    ETF();
    global $ans, $n, $phi;
     
    for ($i = 1; $i <= $n; $i++)
    {
 
        // Summation of d * ETF(d) where
        // d belongs to set of divisors of n
        for ($j = $i; $j <= $n; $j += $i)
        {
            $ans[$j] += ($i * $phi[$i]);
        }
    }
 
    $answer = $ans[$m];
    $answer = ($answer + 1) * $m;
    $answer = (int)($answer / 2);
    return $answer;
}
 
// Driver code
$m = 5;
 
echo LcmSum($m);
 
// This code is contributed by chandan_jnu
?>


Javascript




<script>
 
// javascript implementation of the approach   
var n = 1000002;
 
    var phi = Array(n + 2).fill(0);
    var ans = Array(n + 2).fill(0);
 
    // Euler totient Function
    function ETF() {
        for (i = 1; i <= n; i++) {
            phi[i] = i;
        }
 
        for (i = 2; i <= n; i++) {
            if (phi[i] == i) {
                phi[i] = i - 1;
                for (j = 2 * i; j <= n; j += i) {
                    phi[j] = (phi[j] * (i - 1)) / i;
                }
            }
        }
    }
 
    // Function to return the required LCM sum
    function LcmSum(m) {
        ETF();
 
        for (i = 1; i <= n; i++) {
 
            // Summation of d * ETF(d) where
            // d belongs to set of divisors of n
            for (j = i; j <= n; j += i) {
                ans[j] += (i * phi[i]);
            }
        }
 
        var answer = ans[m];
        answer = (answer + 1) * m;
        answer = answer / 2;
        return answer;
    }
 
    // Driver code
        var m = 5;
        document.write(LcmSum(m));
 
// This code is contributed by aashish1995
</script>


Output: 

55

 

Time Complexity: O(N * logN)
Auxiliary Space: O(N)



Previous Article
Next Article

Similar Reads

Find triplet (A, B, C) such that LCM(A, B) + LCM(A, C) + LCM(B, C) equals N
Given an integer N. Find a triplet let say {A, B, C} such that LCM(A, B) + LCM(B, C) + LCM(A, C) = N. If no possible triplets are there, output -1 instead. Examples: Input: N = 1Output: -1Explanation: No possible triplets are there. Input: N = 15Output: 5 5 5Explanation: LCM(5, 5) + LCM(5, 1) + LCM(5, 1) = 5 + 5 + 5 = 15. Observations: Observation
8 min read
Minimum replacement of pairs by their LCM required to reduce given array to its LCM
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of pairs (arr[i], arr[j]) from the given array needed to be replaced with their LCM such that the array is reduced to a single element equal to the LCM of the initial array.Examples: Input: arr[] = {1, 2, 3, 4} Output: 3 Explanation: LCM of the array = 12
9 min read
Print any pair of integers with sum of GCD and LCM equals to N
Given an integer N, the task is to print any pair of integers that have the sum of GCD and LCM equal to N.Examples: Input: N = 14 Output: 1, 13 Explanation: For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14. Input: 2 Output: 1 1 Explanation: For the given pair we have GCD(1, 1) = 1 and LCM (1, 1) = 1.
3 min read
Count of all possible pairs having sum of LCM and GCD equal to N
Given an integer N, the task is to find the count of all possible pairs of integers (A, B) such that GCD (A, B) + LCM (A, B) = N. Examples: Input: N = 14Output: 7Explanation: All the possible pairs are {1, 13}, {2, 12}, {4, 6}, {6, 4}, {7, 7}, {12, 2}, {13, 1} Input: N = 6Output: 5 Approach:Follow the steps below to solve the problem: Initialize a
5 min read
Minimize sum of K positive integers with given LCM
Given two positive integers K and X, the task is to find the minimum possible sum of K positive integers ( repetitions allowed ) having LCM X. Examples: Input: K = 2, X = 6 Output: 5 Explanation: K(= 2) positive integers of minimum possible sum having LCM X(= 6) are { 2, 3 }. Therefore, the minimum possible sum of K(= 2) positive integers = (2 + 3)
14 min read
Print triplet with sum equal to N and LCM at most N/2
Given a positive integer N, the task is to find a triplet {X, Y, Z} such that the least common multiple of X, Y, and Z is less than or equal to N/2 and the sum of X, Y, and Z is equal to N. If there exist multiple answers, then print any of them. Examples: Input: N = 8Output: 4 2 2Explanation: One possible triplet is {4, 2, 2}. The sum of all the i
7 min read
Find the sum of product of power and LCM for N operations
Given an integer N, the task is to find the value after performing the following N operation when i is 1 initially: Find the value of i raised to the power of (N - i) and then multiply it with the lcm of i and N.Add this value to the final answer. Increment i by 1. Examples: Input: 5 Output: 305 Explanation: Following are the steps of the operation
5 min read
Find two pairs such that one's GCD is same as other's LCM and sum equal to N
Given an integer N, the task is to find two pairs of positive integers such that the GCD of the first pair is the same as the LCM of the second pair and the sum of the values is equal to N. Note: If multiple outputs possible, print any of them Examples: Input: N = 9Output: 6 1 1 1Explanation: The GCD(6, 1) = 1 and LCM(1, 1) = 1, GCD is equal to LCM
5 min read
Find two numbers with given sum and maximum possible LCM
Given an integer X, the task is to find two integers A and B such that sum of these two numbers is X and the LCM of A and B is maximum. Examples: Input: X = 15Output: 7 8Explanation: 7 + 8 = 15 and LCM(7, 8) = 56 is the maximum possible. Input: X = 30 Output: 13 17Explanation:13 + 17 = 30 and LCM(13, 17) = 221 is the maximum possible. Naive Approac
10 min read
Maximum sum of distinct numbers with LCM as N
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 nu
6 min read
Maximum sum of distinct numbers such that LCM of these numbers is N
Given a positive number N. The task is to find the maximum sum of distinct numbers such that the LCM of all these numbers is equal to N. Examples: Input : 2 Output : 3 The distinct numbers you can have are just 1 and 2 and their sum is equal to 3. Input : 5 Output : 6Recommended PracticeMaximum Sum LCMTry It! As the LCM of all the numbers is N. So
4 min read
Maximum sum subarray having sum less than given sum using Set
Given an array arr[] of length N and an integer K, the task is the find the maximum sum subarray with a sum less than K.Note: If K is less than the minimum element, then return INT_MIN. Examples: Input: arr[] = {-1, 2, 2}, K = 4 Output: 3 Explanation: The subarray with maximum sum which is less than 4 is {-1, 2, 2}. The subarray {2, 2} has maximum
7 min read
Maximum sum subarray having sum less than or equal to given sum
Given an array of non-negative integers and a sum. We have to find sum of the subarray having a maximum sum less than or equal to the given sum in the array. Note: Given array contains only non-negative integers. Examples: Input: arr[] = { 1, 2, 3, 4, 5 } sum = 11Output: 10Subarray having maximum sum is { 1, 2, 3, 4 }Input: arr[] = { 2, 4, 6, 8, 10
10 min read
Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
Given an integer N, find out the count of numbers(m) that satisfy the condition m + sum(m) + sum (sum(m)) = N, where sum(m) denotes the sum of digits in m. Given N &lt;= 10e9. Examples: Input: 9 Output: 1 Explanation: Only 1 positive integer satisfies the condition that is 3, 3 + sum(3) + sum(sum(3)) = 3 + 3 + 3 = 9 Input: 9939 Output: 4 Explanatio
9 min read
Count pairs in an array such that LCM(arr[i], arr[j]) &gt; min(arr[i],arr[j])
Given an array arr[], the task is to find the count of pairs in the array such that LCM(arr[i], arr[j]) &gt; min(arr[i], arr[j]) Note: Pairs (arr[i], arr[j]) and (arr[j], arr[i]) are considered identical and will be counted only once. Examples: Input: arr[] = {1, 1, 4, 9} Output: 5 All valid pairs are (1, 4), (1, 9), (1, 4), (1, 9) and (4, 9). Inpu
9 min read
Levelwise Alternating GCD and LCM of nodes in Segment Tree
A Levelwise GCD/LCM alternating segment tree is a segment tree, such that at every level the operations GCD and LCM alternate. In other words at Level 1 the left and right sub-trees combine together by the GCD operation i.e Parent node = Left Child GCD Right Child and on Level 2 the left and right sub-trees combine together by the LCM operation i.e
15+ min read
LCM of two large numbers
Given two large numbers 'a' and 'b' such that(10^20&lt;=a, b&lt;=10^300). Find the LCM of two large numbers given. Examples: Input: a = 234516789234023485693020129 b = 176892058718950472893785940 Output: 41484157651764614525905399263631111992263435437186260 Input: a = 36594652830916364940473625749407 b = 448507083624364748494746353648484939 Output:
3 min read
Program to find LCM of 2 numbers without using GCD
Finding LCM using GCD is explained here but here the task is to find LCM without first calculating GCD.Examples: Input: 7, 5 Output: 35 Input: 2, 6 Output: 6 The approach is to start with the largest of the 2 numbers and keep incrementing the larger number by itself till smaller number perfectly divides the resultant. C/C++ Code // C++ program to f
4 min read
Find any pair with given GCD and LCM
Given gcd G and lcm L. The task is to print any pair which has gcd G and lcm L.Examples: Input: G = 3, L = 12 Output: 3, 12 Input: G = 1, L = 10 Output: 1, 10 A normal solution will be to perform iteration over all the factor pairs of g*l and check if any pair has gcd g and lcm as l. If they have, then the pair will be the answer.Below is the imple
8 min read
Number of distinct integers obtained by lcm(X, N)/X
Given a number N, find the number of distinct integers obtained by LCM(X, N)/X where X can be any positive number. Examples: Input: N = 2 Output: 2 if X is 1, then lcm(1, 2)/1 is 2/1=2. if X is 2, then lcm(2, 2)/2 is 2/2=1. For any X greater than 2 we cannot obtain a distinct integer. Input: N = 3 Output: 2 It is known that LCM(x, y) = x*y/GCD(x, y
4 min read
Find the other number when LCM and HCF given
Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60 = 15 * 12. How does this formula work? Since HCF
4 min read
LCM and HCF of fractions
Given n fractions as two arrays Num and Den. The task is to find out the L.C.M of the fractions. Examples: Input: num[] = {1, 7, 4}, den[] = {2, 3, 6} Output: LCM is = 28/1 The given fractions are 1/2, 7/3 and 4/6. The LCM is 28/1 Input: num[] = {24, 48, 72, 96}, den[] = {2, 6, 8, 3} Output: LCM is = 288/1 LCM of A/B and C/D = (LCM of A and C) / (H
13 min read
LCM of N numbers modulo M
Given an array arr[] of integers, the task is to find the LCM of all the elements of the array modulo M where M = 109 + 7.Examples: Input: arr[] = {10000000, 12345, 159873} Output: 780789722 LCM of (10000000, 12345, 159873) is 1315754790000000 1315754790000000 % 1000000007 = 780789722Input: arr[] = {10, 13, 15} Output: 390 Approach: If you have gon
11 min read
Maximum length subarray with LCM equal to product
Given an arr[], the task is to find the maximum length of the sub-array such that the LCM of the sub-array is equal to the product of numbers in the sub-array. If no valid sub-array exists, then print -1. Note: The length of the sub-array must be ? 2. Examples: Input: arr[] = { 6, 10, 21} Output: 2 The sub-array { 10, 21 } satisfies the condition.
15+ min read
Minimum LCM and GCD possible among all possible sub-arrays
Given an array arr[] of N positive integers, the task is to find the minimum LCM and GCD between elements of all possible sub-array.Examples: Input: arr[] = {4, 4, 8} Output: LCM = 4, GCD = 4 All possible sub-arrays are: {4} -&gt; LCM = 4, GCD = 4 {8} -&gt; LCM = 8, GCD = 8 {4, 8} -&gt; LCM = 8, GCD = 4Input: arr[] = {2, 66, 14, 521} Output: LCM =
6 min read
Count the number of subsequences of length k having equal LCM and HCF
Given an array Arr and an integer K. The task is to find the number of subsequences of size K such that the LCM and HCF of the sequence is same.Examples: Input: Arr = {1, 2, 2, 3, 3}, K = 2 Output: 2Subsequences are - {2, 2} and {3, 3}Input: Arr = {1, 1, 1, 1, 2, 2}, K = 3 Output: 4 Approach: LCM and HCF ( GCD ) are equal for a group of numbers whe
7 min read
Maximum LCM among all pairs (i, j) of first N natural numbers
Given a positive integer N &gt; 1, the task is to find the maximum LCM among all the pairs (i, j) such that i &lt; j ? N.Examples: Input: N = 3 Output: 6 LCM(1, 2) = 2 LCM(1, 3) = 3 LCM(2, 3) = 6Input: N = 4 Output: 12 Approach: Since the LCM of two consecutive elements is equal to their multiples then it is obvious that the maximum LCM will be of
2 min read
Longest sub-sequence with minimum LCM
Given an array arr[] of length N, the task is to find the length of the longest sub-sequence with minimum possible LCM.Examples: Input: arr[] = {1, 3, 1} Output: 2 {1} and {1} are the subsequences with the minimum possible LCM.Input: arr[] = {3, 4, 5, 3, 2, 3} Output: 1 {2} is the required subsequence. Approach: The minimum possible LCM from the ar
4 min read
Remove an element to minimize the LCM of the given array
Given an array arr[] of length N ? 2. The task is to remove an element from the given array such that the LCM of the array after removing it is minimized.Examples: Input: arr[] = {18, 12, 24} Output: 24 Remove 12: LCM(18, 24) = 72 Remove 18: LCM(12, 24) = 24 Remove 24: LCM(12, 18) = 36Input: arr[] = {5, 15, 9, 36} Output: 45 Approach: Idea is to fi
8 min read
Minimum possible value of max(A, B) such that LCM(A, B) = C
Given an integer C, the task is to find the minimum possible value of max(A, B) such that LCM(A, B) = C.Examples: Input: C = 6 Output: 3 max(1, 6) = 6 max(2, 3) = 3 and min(6, 3) = 3Input: C = 9 Output: 9 Approach: An approach to solve this problem is to find all the factors of the given number using the approach discussed in this article and then
6 min read
three90RightbarBannerImg