📌  相关文章
📜  可被给定数组中所有数字整除的最大 K 位数字

📅  最后修改于: 2022-05-13 01:56:09.722000             🧑  作者: Mango

可被给定数组中所有数字整除的最大 K 位数字

给定一个大小为N的数组arr[]和一个整数K。任务是找到可被所有 arr[]数整除的最大 K 位数

例子:

方法:该解决方案基于类似于找到可被 X 整除的最大 K 位数的想法。按照下面提到的步骤。

  • 查找所有数组 arr[] 的 LCM
  • 使用以下公式找到具有 K 位数字的 LCM 的最大倍数:

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
   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 find LCM of the array
  int findLCM(int arr[], int n, int idx)
  {
 
    // lcm(a, b) = (a*b / gcd(a, b))
    if (idx == n - 1)
    {
      return arr[idx];
    }
    int a = arr[idx];
    int b = findLCM(arr, n, idx + 1);
 
    double gcd = __gcd(a, b);
 
    return (a * (int)floor(b / gcd));
  }
 
  // Function to find the number
  int findNum(int arr[], int n, int K)
  {
    int  lcm = findLCM(arr, n, 0);
    int ans = (int)floor((pow(10, K) - 1) / lcm);
    ans = (ans) * lcm;
 
    if (ans == 0)
      return -1;
 
    return ans;
  }
 
  // Driver code
  int main()
  {
    int arr[] = { 2, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
 
    cout << findNum(arr, n, K);
  }
 
// This code is contributed by Samim Hossain Mondal.


Java
// Java code to implement above approach
class GFG
{
  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 find LCM of the array
  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);
 
    double gcd = __gcd(a, b);
 
    return (a * (int)Math.floor(b / gcd));
  }
 
  // Function to find the number
  static int findNum(int []arr, int K)
  {
    int  lcm = findLCM(arr, 0);
    int ans = (int)Math.floor((Math.pow(10, K) - 1) / lcm);
    ans = (ans) * lcm;
 
    if (ans == 0)
      return -1;
 
    return ans;
  }
 
  // Driver code
  public static void main(String []args)
  {
    int []arr = { 2, 3, 5 };
    int K = 3;
 
    System.out.println(findNum(arr, K));
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# python code to implement above approach
import math
 
# Function to find LCM of the array
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))
 
# Function to find the number
def findNum(arr, K):
 
    lcm = findLCM(arr, 0)
    ans = (pow(10, K) - 1) // lcm
    ans = (ans)*lcm
    if (ans == 0):
        return -1
    return ans
 
# Driver code
if __name__ == "__main__":
 
    arr = [2, 3, 5]
    K = 3
    print(findNum(arr, K))
 
# This code is contributed by rakeshsahni


C#
// C# code to implement above approach
using System;
using System.Collections;
class GFG
{
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 find LCM of the array
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);
     
    double gcd = __gcd(a, b);
     
    return (a * (int)Math.Floor(b / gcd));
}
 
// Function to find the number
static int findNum(int []arr, int K)
{
    int  lcm = findLCM(arr, 0);
    int ans = (int)Math.Floor((Math.Pow(10, K) - 1) / lcm);
    ans = (ans) * lcm;
     
    if (ans == 0)
        return -1;
         
    return ans;
}
 
// Driver code
public static void Main()
{
int []arr = { 2, 3, 5 };
int K = 3;
 
Console.Write(findNum(arr, K));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
990

时间复杂度: O(N*logD) 其中 D 是数组的最大元素
辅助空间: O(1)