Smallest K digit number divisible by all numbers in given array - GeeksforGeeks

17 Dec, 2021
Given an array arr[]. The task is to create the smallest K digit number divisible by all numbers of arr[].
Examples:
Input: arr[] = {2, 3, 5}, N = 3Output: 120Explanation: 120 is divisible by 2, 3 and 5
Input: arr[] = {2, 6, 7, 4, 5}, N = 5Output: 10080
Approach: This problem can be solved by using Lowest common multiple. Follow the steps below to solve the given problem. 
Find the LCM of all the array elements of arr[].
Find a multiple of LCM having K digits.
The first number having K digits will be the final answer.
Finally, return the answer.
Below is the implementation of the above approach. 
C++
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Recursive implementationint findLCM(vector<int> 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 conditionsint findNum(vector<int>& arr, int N){    int lcm = findLCM(arr, 0);    int ans = pow(10, N - 1) / lcm;    ans = (ans + 1) * lcm;    return ans;} // Driver Codeint main(){      // Array arr[]    vector<int> arr = { 2, 3, 5 };    int N = 3;         // Function call    cout << findNum(arr, N);       return 0;}
// Java program for above approachclass 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.
# Python 3 program for above approachimport math# Recursive implementationdef 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 Codeif __name__ == "__main__":     # Array arr[]    arr = [2, 3, 5]    N = 3     # Function call    print(findNum(arr, N))     # This code is contributed by ukasp.
// C# program for above approachusing 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.
<script>        // JavaScript code for the above approach        function __gcd(a, b) {             // Everything divides 0            if (b == 0) {                return a;            }             return __gcd(b, a % b);        }                 // Recursive implementation        function 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 (a * b / __gcd(a, b));        }         // Finding smallest number with given conditions        function findNum(arr, N) {            let lcm = findLCM(arr, 0);            let ans = Math.floor(Math.pow(10, N - 1) / lcm);            ans = (ans + 1) * lcm;            return ans;        }         // Driver Code         // Array arr[]        let arr = [2, 3, 5];        let N = 3;         // Function call        document.write(findNum(arr, N));   // This code is contributed by Potta Lokesh    </script>
  
120
 
Time Complexity: O(N) Auxiliary Space: O(1)
 
lokeshpotta20
ukasp
_saurabh_jaiswal
gaurav01
divisibility
GCD-LCM
number-digits
Greedy
Mathematical
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
Program for First Fit algorithm in Memory Management
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Program to find GCD or HCF of two numbers