给定大小为N的数组arr [] ,任务是查找给定数组中所有唯一对的最小LCM(最小公倍数),其中1 <= N <= 10 5,1 <= arr [i] < = 10 5 。
例子:
Input: arr[] = {2, 4, 3}
Output: 4
Explanation
LCM (2, 4) = 4
LCM (2, 3) = 6
LCM (4, 3) = 12
Minimum possible LCM is 4.
Input: arr [] ={1, 5, 2, 2, 6}
Output: 2
天真的方法
- 生成所有可能的对,并为每个唯一对计算LCM。
- 从所有唯一对中找到最小的LCM。
下面是上述方法的实现:
C++
// C++ program to find
// minimum possible lcm
// from any pair
#include
using namespace std;
// function to compute
// GCD of two numbers
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// function that return
// minimum possible lcm
// from any pair
int minLCM(int arr[], int n)
{
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
// fix the ith element and
// iterate over all the array
// to find minimum LCM
for (int j = i + 1; j < n; j++) {
int g = gcd(arr[i], arr[j]);
int lcm = arr[i] / g * arr[j];
ans = min(ans, lcm);
}
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 3, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minLCM(arr, n) << endl;
return 0;
}
Java
// Java program to find minimum
// possible lcm from any pair
import java.io.*;
import java.util.*;
class GFG {
// Function to compute
// GCD of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function that return minimum
// possible lcm from any pair
static int minLCM(int arr[], int n)
{
int ans = Integer.MAX_VALUE;
for(int i = 0; i < n; i++)
{
// Fix the ith element and
// iterate over all the array
// to find minimum LCM
for(int j = i + 1; j < n; j++)
{
int g = gcd(arr[i], arr[j]);
int lcm = arr[i] / g * arr[j];
ans = Math.min(ans, lcm);
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 4, 3, 6, 5 };
int n = arr.length;
System.out.println(minLCM(arr,n));
}
}
// This code is contributed by coder001
Python3
# Python3 program to find minimum
# possible lcm from any pair
import sys
# Function to compute
# GCD of two numbers
def gcd(a, b):
if (b == 0):
return a;
return gcd(b, a % b);
# Function that return minimum
# possible lcm from any pair
def minLCM(arr, n):
ans = 1000000000;
for i in range(n):
# Fix the ith element and
# iterate over all the
# array to find minimum LCM
for j in range(i + 1, n):
g = gcd(arr[i], arr[j]);
lcm = arr[i] / g * arr[j];
ans = min(ans, lcm);
return ans;
# Driver code
arr = [ 2, 4, 3, 6, 5 ];
print(minLCM(arr, 5))
# This code is contributed by grand_master
C#
// C# program to find minimum
// possible lcm from any pair
using System;
class GFG{
// Function to compute
// GCD of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function that return minimum
// possible lcm from any pair
static int minLCM(int []arr, int n)
{
int ans = Int32.MaxValue;
for(int i = 0; i < n; i++)
{
// Fix the ith element and
// iterate over all the array
// to find minimum LCM
for(int j = i + 1; j < n; j++)
{
int g = gcd(arr[i], arr[j]);
int lcm = arr[i] / g * arr[j];
ans = Math.Min(ans, lcm);
}
}
return ans;
}
// Driver code
public static void Main()
{
int []arr = { 2, 4, 3, 6, 5 };
int n = arr.Length;
Console.Write(minLCM(arr,n));
}
}
// This code is contributed by Akanksha_Rai
Javascript
C++
// C++ program to find the
// pair having minimum LCM
#include
using namespace std;
// function that return
// pair having minimum LCM
int minLCM(int arr[], int n)
{
int mx = 0;
for (int i = 0; i < n; i++) {
// find max element in the array as
// the gcd of two elements from the
// array can't greater than max element.
mx = max(mx, arr[i]);
}
// created a 2D array to store minimum
// two multiple of any particular i.
vector > mul(mx + 1);
for (int i = 0; i < n; i++) {
if (mul[arr[i]].size() > 1) {
// we already found two
// smallest multiple
continue;
}
mul[arr[i]].push_back(arr[i]);
}
// iterating over all gcd
for (int i = 1; i <= mx; i++) {
// iterating over its multiple
for (int j = i + i; j <= mx; j += i) {
if (mul[i].size() > 1) {
// if we already found the
// two smallest multiple of i
break;
}
for (int k : mul[j]) {
if (mul[i].size() > 1)
break;
mul[i].push_back(k);
}
}
}
int ans = INT_MAX;
for (int i = 1; i <= mx; i++) {
if (mul[i].size() <= 1)
continue;
// choosing smallest two multiple
int a = mul[i][0], b = mul[i][1];
// calculating lcm
int lcm = (a * b) / i;
ans = min(ans, lcm);
}
// return final answer
return ans;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 3, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minLCM(arr, n) << endl;
return 0;
}
Java
// Java program to find the
// pair having minimum LCM
import java.util.Vector;
class GFG{
// Function that return
// pair having minimum LCM
static int minLCM(int arr[],
int n)
{
int mx = 0;
for (int i = 0; i < n; i++)
{
// Find max element in the
// array as the gcd of two
// elements from the array
// can't greater than max element.
mx = Math.max(mx, arr[i]);
}
// Created a 2D array to store minimum
// two multiple of any particular i.
Vector []mul = new Vector[mx + 1];
for (int i = 0; i < mul.length; i++)
mul[i] = new Vector();
for (int i = 0; i < n; i++)
{
if (mul[arr[i]].size() > 1)
{
// We already found two
// smallest multiple
continue;
}
mul[arr[i]].add(arr[i]);
}
// Iterating over all gcd
for (int i = 1; i <= mx; i++)
{
// Iterating over its multiple
for (int j = i + i; j <= mx; j += i)
{
if (mul[i].size() > 1)
{
// If we already found the
// two smallest multiple of i
break;
}
for (int k : mul[j])
{
if (mul[i].size() > 1)
break;
mul[i].add(k);
}
}
}
int ans = Integer.MAX_VALUE;
for (int i = 1; i <= mx; i++)
{
if (mul[i].size() <= 1)
continue;
// Choosing smallest
// two multiple
int a = mul[i].get(0),
b = mul[i].get(1);
// Calculating lcm
int lcm = (a * b) / i;
ans = Math.min(ans, lcm);
}
// Return final answer
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {2, 4, 3, 6, 5};
int n = arr.length;
System.out.print(minLCM(arr, n) + "\n");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to find the
# pair having minimum LCM
import sys
# function that return
# pair having minimum LCM
def minLCM(arr, n) :
mx = 0
for i in range(n) :
# find max element in the array as
# the gcd of two elements from the
# array can't greater than max element.
mx = max(mx, arr[i])
# created a 2D array to store minimum
# two multiple of any particular i.
mul = [[] for i in range(mx + 1)]
for i in range(n) :
if (len(mul[arr[i]]) > 1) :
# we already found two
# smallest multiple
continue
mul[arr[i]].append(arr[i])
# iterating over all gcd
for i in range(1, mx + 1) :
# iterating over its multiple
for j in range(i + i, mx + 1, i) :
if (len(mul[i]) > 1) :
# if we already found the
# two smallest multiple of i
break
for k in mul[j] :
if (len(mul[i]) > 1) :
break
mul[i].append(k)
ans = sys.maxsize
for i in range(1, mx + 1) :
if (len(mul[i]) <= 1) :
continue
# choosing smallest two multiple
a, b = mul[i][0], mul[i][1]
# calculating lcm
lcm = (a * b) // i
ans = min(ans, lcm)
# return final answer
return ans
# Driver code
arr = [ 2, 4, 3, 6, 5 ]
n = len(arr)
print(minLCM(arr, n))
# This code is contributed by divyesh072019
C#
// C# program to find the
// pair having minimum LCM
using System;
using System.Collections.Generic;
class GFG{
// Function that return
// pair having minimum LCM
static int minLCM(int []arr,
int n)
{
int mx = 0;
for (int i = 0; i < n; i++)
{
// Find max element in the
// array as the gcd of two
// elements from the array
// can't greater than max element.
mx = Math.Max(mx, arr[i]);
}
// Created a 2D array to store minimum
// two multiple of any particular i.
List []mul = new List[mx + 1];
for (int i = 0; i < mul.Length; i++)
mul[i] = new List();
for (int i = 0; i < n; i++)
{
if (mul[arr[i]].Count > 1)
{
// We already found two
// smallest multiple
continue;
}
mul[arr[i]].Add(arr[i]);
}
// Iterating over all gcd
for (int i = 1; i <= mx; i++)
{
// Iterating over its multiple
for (int j = i + i; j <= mx; j += i)
{
if (mul[i].Count > 1)
{
// If we already found the
// two smallest multiple of i
break;
}
foreach (int k in mul[j])
{
if (mul[i].Count > 1)
break;
mul[i].Add(k);
}
}
}
int ans = int.MaxValue;
for (int i = 1; i <= mx; i++)
{
if (mul[i].Count <= 1)
continue;
// Choosing smallest
// two multiple
int a = mul[i][0],
b = mul[i][1];
// Calculating lcm
int lcm = (a * b) / i;
ans = Math.Min(ans, lcm);
}
// Return readonly answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {2, 4, 3, 6, 5};
int n = arr.Length;
Console.Write(minLCM(arr, n) + "\n");
}
}
// This code is contributed by Princi Singh
输出:
4
时间复杂度: O(N 2 )
高效方法:此方法取决于以下公式:
Product of two number = LCM of two number * GCD of two number
- 在LCM公式中,分母是两个数字的GCD,两个数字的GCD永远不会大于数字本身。
- 因此,对于固定GCD,找到给定数组中存在的固定GCD的最小两个倍数。
- 仅存储每个GCD的最小的两个倍数,因为选择数组中存在的更大的GCD的倍数,无论如何,它将永远不会给出最小的答案。
- 最后,使用筛子查找最小的两个数字,该数字是所选GCD的倍数。
下面是上述方法的实现:
C++
// C++ program to find the
// pair having minimum LCM
#include
using namespace std;
// function that return
// pair having minimum LCM
int minLCM(int arr[], int n)
{
int mx = 0;
for (int i = 0; i < n; i++) {
// find max element in the array as
// the gcd of two elements from the
// array can't greater than max element.
mx = max(mx, arr[i]);
}
// created a 2D array to store minimum
// two multiple of any particular i.
vector > mul(mx + 1);
for (int i = 0; i < n; i++) {
if (mul[arr[i]].size() > 1) {
// we already found two
// smallest multiple
continue;
}
mul[arr[i]].push_back(arr[i]);
}
// iterating over all gcd
for (int i = 1; i <= mx; i++) {
// iterating over its multiple
for (int j = i + i; j <= mx; j += i) {
if (mul[i].size() > 1) {
// if we already found the
// two smallest multiple of i
break;
}
for (int k : mul[j]) {
if (mul[i].size() > 1)
break;
mul[i].push_back(k);
}
}
}
int ans = INT_MAX;
for (int i = 1; i <= mx; i++) {
if (mul[i].size() <= 1)
continue;
// choosing smallest two multiple
int a = mul[i][0], b = mul[i][1];
// calculating lcm
int lcm = (a * b) / i;
ans = min(ans, lcm);
}
// return final answer
return ans;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 3, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minLCM(arr, n) << endl;
return 0;
}
Java
// Java program to find the
// pair having minimum LCM
import java.util.Vector;
class GFG{
// Function that return
// pair having minimum LCM
static int minLCM(int arr[],
int n)
{
int mx = 0;
for (int i = 0; i < n; i++)
{
// Find max element in the
// array as the gcd of two
// elements from the array
// can't greater than max element.
mx = Math.max(mx, arr[i]);
}
// Created a 2D array to store minimum
// two multiple of any particular i.
Vector []mul = new Vector[mx + 1];
for (int i = 0; i < mul.length; i++)
mul[i] = new Vector();
for (int i = 0; i < n; i++)
{
if (mul[arr[i]].size() > 1)
{
// We already found two
// smallest multiple
continue;
}
mul[arr[i]].add(arr[i]);
}
// Iterating over all gcd
for (int i = 1; i <= mx; i++)
{
// Iterating over its multiple
for (int j = i + i; j <= mx; j += i)
{
if (mul[i].size() > 1)
{
// If we already found the
// two smallest multiple of i
break;
}
for (int k : mul[j])
{
if (mul[i].size() > 1)
break;
mul[i].add(k);
}
}
}
int ans = Integer.MAX_VALUE;
for (int i = 1; i <= mx; i++)
{
if (mul[i].size() <= 1)
continue;
// Choosing smallest
// two multiple
int a = mul[i].get(0),
b = mul[i].get(1);
// Calculating lcm
int lcm = (a * b) / i;
ans = Math.min(ans, lcm);
}
// Return final answer
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {2, 4, 3, 6, 5};
int n = arr.length;
System.out.print(minLCM(arr, n) + "\n");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to find the
# pair having minimum LCM
import sys
# function that return
# pair having minimum LCM
def minLCM(arr, n) :
mx = 0
for i in range(n) :
# find max element in the array as
# the gcd of two elements from the
# array can't greater than max element.
mx = max(mx, arr[i])
# created a 2D array to store minimum
# two multiple of any particular i.
mul = [[] for i in range(mx + 1)]
for i in range(n) :
if (len(mul[arr[i]]) > 1) :
# we already found two
# smallest multiple
continue
mul[arr[i]].append(arr[i])
# iterating over all gcd
for i in range(1, mx + 1) :
# iterating over its multiple
for j in range(i + i, mx + 1, i) :
if (len(mul[i]) > 1) :
# if we already found the
# two smallest multiple of i
break
for k in mul[j] :
if (len(mul[i]) > 1) :
break
mul[i].append(k)
ans = sys.maxsize
for i in range(1, mx + 1) :
if (len(mul[i]) <= 1) :
continue
# choosing smallest two multiple
a, b = mul[i][0], mul[i][1]
# calculating lcm
lcm = (a * b) // i
ans = min(ans, lcm)
# return final answer
return ans
# Driver code
arr = [ 2, 4, 3, 6, 5 ]
n = len(arr)
print(minLCM(arr, n))
# This code is contributed by divyesh072019
C#
// C# program to find the
// pair having minimum LCM
using System;
using System.Collections.Generic;
class GFG{
// Function that return
// pair having minimum LCM
static int minLCM(int []arr,
int n)
{
int mx = 0;
for (int i = 0; i < n; i++)
{
// Find max element in the
// array as the gcd of two
// elements from the array
// can't greater than max element.
mx = Math.Max(mx, arr[i]);
}
// Created a 2D array to store minimum
// two multiple of any particular i.
List []mul = new List[mx + 1];
for (int i = 0; i < mul.Length; i++)
mul[i] = new List();
for (int i = 0; i < n; i++)
{
if (mul[arr[i]].Count > 1)
{
// We already found two
// smallest multiple
continue;
}
mul[arr[i]].Add(arr[i]);
}
// Iterating over all gcd
for (int i = 1; i <= mx; i++)
{
// Iterating over its multiple
for (int j = i + i; j <= mx; j += i)
{
if (mul[i].Count > 1)
{
// If we already found the
// two smallest multiple of i
break;
}
foreach (int k in mul[j])
{
if (mul[i].Count > 1)
break;
mul[i].Add(k);
}
}
}
int ans = int.MaxValue;
for (int i = 1; i <= mx; i++)
{
if (mul[i].Count <= 1)
continue;
// Choosing smallest
// two multiple
int a = mul[i][0],
b = mul[i][1];
// Calculating lcm
int lcm = (a * b) / i;
ans = Math.Min(ans, lcm);
}
// Return readonly answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {2, 4, 3, 6, 5};
int n = arr.Length;
Console.Write(minLCM(arr, n) + "\n");
}
}
// This code is contributed by Princi Singh
输出:
4
时间复杂度: O((N + M)* log(M))
辅助空间: O(M) ,其中M是数组中的最大元素。