Largest K digit number divisible by all numbers in given array

15 Dec, 2021
Given an array arr[] of size N and an integer K. The task is to find the largest K digit number divisible by all number of arr[].
Examples:
Input: arr[] = {2, 3, 5}, K = 3Output: 990Explanation: 990 is the largest 3 digit number divisible by 2, 3 and 5.
Input: arr[] = {91, 93, 95}, K = 3Output: -1Explanation: There is no 3 digit number divisible by all 91, 93 and 95.
Approach: The solution is based on the idea similar to finding largest K digit number divisible by X. Follow the steps mentioned below.
Find LCM of all numbers of array arr[]
Find the largest multiple of LCM having K digits using the formula:
LCM(arr) * ((10K-1)/LCM(arr))
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ code to implement above approach#include <bits/stdc++.h>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 code to implement above approachclass 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
# python code to implement above approachimport math # Function to find LCM of the arraydef 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 numberdef findNum(arr, K):     lcm = findLCM(arr, 0)    ans = (pow(10, K) - 1) // lcm    ans = (ans)*lcm    if (ans == 0):        return -1    return ans # Driver codeif __name__ == "__main__":     arr = [2, 3, 5]    K = 3    print(findNum(arr, K)) # This code is contributed by rakeshsahni
// C# code to implement above approachusing 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 arraystatic 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 numberstatic 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 codepublic static void Main(){int []arr = { 2, 3, 5 };int K = 3; Console.Write(findNum(arr, K));}} // This code is contributed by Samim Hossain Mondal.
<script> // JavaScript code to implement above approachfunction __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 find LCM of the arrayfunction findLCM(arr, idx){         // lcm(a, b) = (a*b / gcd(a, b))    if (idx == arr.length - 1)    {        return arr[idx];    }    let a = arr[idx];    let b = findLCM(arr, idx + 1);    return Math.floor((a * b / __gcd(a, b)));} // Function to find the numberfunction findNum(arr, K){    let lcm = findLCM(arr, 0);    let ans = Math.floor((Math.pow(10, K) - 1) / lcm);    ans = (ans) * lcm;         if (ans == 0)        return -1;             return ans;} // Driver codelet arr = [ 2, 3, 5 ];let K = 3; document.write(findNum(arr, K)); // This code is contributed by Potta Lokesh </script>
990
Time Complexity: O(N*logD) where D is the maximum element of the arrayAuxiliary Space: O(1)
 
lokeshpotta20
rakeshsahni
samim2000
29AjayKumar
GCD-LCM
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Write a program to print all permutations of a given string
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3