Skip to content
Related Articles

Related Articles

Smallest K digit number divisible by all numbers in given array

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Dec, 2021
View Discussion
Improve Article
Save Article

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 = 3
Output: 120
Explanation: 120 is divisible by 2, 3 and 5

Input: arr[] = {2, 6, 7, 4, 5}, N = 5
Output: 10080

Approach: This problem can be solved by using Lowest common multiple. Follow the steps below to solve the given problem. 

Below is the implementation of the above approach. 

C++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Recursive implementation
int 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 conditions
int 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 Code
int main()
{
      // Array arr[]
    vector<int> 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




<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>


 
 

Output

120

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!