可被给定数组中所有数字整除的最小 K 位数字
给定一个数组arr[] 。任务是创建可被arr[]的所有数字整除的最小K位数字。
例子:
Input: arr[] = {2, 3, 5}, N = 3
Output: 120
Explanation: 120 is divisible by 2, 3 and 5
Input: arr[] = {2, 6, 7, 4, 5}, N = 5
Output: 10080
方法:这个问题可以用最小公倍数来解决。请按照以下步骤解决给定的问题。
- 找到arr[]的所有数组元素的 LCM。
- 找到具有K个数字的LCM的倍数。
- 第一个有K位数的数字将是最终答案。
- 最后,返回答案。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Recursive implementation
int findLCM(vector arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.size() - 1) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
// Finding smallest number with given conditions
int findNum(vector& arr, int N)
{
int lcm = findLCM(arr, 0);
int ans = pow(10, N - 1) / lcm;
ans = (ans + 1) * lcm;
return ans;
}
// Driver Code
int main()
{
// Array arr[]
vector arr = { 2, 3, 5 };
int N = 3;
// Function call
cout << findNum(arr, N);
return 0;
}
Java
// Java program for above approach
class GFG {
static int __gcd(int a, int b) {
// Everything divides 0
if (b == 0) {
return a;
}
return __gcd(b, a % b);
}
// Recursive implementation
static int findLCM(int[] arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
// Finding smallest number with given conditions
static int findNum(int[] arr, int N) {
int lcm = findLCM(arr, 0);
int ans = (int) Math.pow(10, N - 1) / lcm;
ans = (ans + 1) * lcm;
return ans;
}
// Driver Code
public static void main(String args[])
{
// Array arr[]
int[] arr = { 2, 3, 5 };
int N = 3;
// Function call
System.out.println(findNum(arr, N));
}
}
// This code is contributed b saurabh_jaiswal.
Python3
# Python 3 program for above approach
import math
# Recursive implementation
def findLCM(arr, idx):
# lcm(a,b) = (a*b/gcd(a,b))
if (idx == len(arr) - 1):
return arr[idx]
a = arr[idx]
b = findLCM(arr, idx + 1)
return (a * b // math.gcd(a, b))
# Finding smallest number with given conditions
def findNum(arr, N):
lcm = findLCM(arr, 0)
ans = pow(10, N - 1) // lcm
ans = (ans + 1) * lcm
return ans
# Driver Code
if __name__ == "__main__":
# Array arr[]
arr = [2, 3, 5]
N = 3
# Function call
print(findNum(arr, N))
# This code is contributed by ukasp.
C#
// C# program for above approach
using System;
class GFG {
static int __gcd(int a, int b) {
// Everything divides 0
if (b == 0) {
return a;
}
return __gcd(b, a % b);
}
// Recursive implementation
static int findLCM(int[] arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.Length - 1) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
// Finding smallest number with given conditions
static int findNum(int[] arr, int N) {
int lcm = findLCM(arr, 0);
int ans = (int) Math.Pow(10, N - 1) / lcm;
ans = (ans + 1) * lcm;
return ans;
}
// Driver Code
public static void Main(string []args)
{
// Array arr[]
int[] arr = { 2, 3, 5 };
int N = 3;
// Function call
Console.WriteLine(findNum(arr, N));
}
}
// This code is contributed by gaurav01.
Javascript
输出
120
时间复杂度: O(N)
辅助空间: O(1)