Open In App

Largest K digit number divisible by all numbers in given array

Last Updated : 15 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 = 3
Output: 990
Explanation: 990 is the largest 3 digit number divisible by 2, 3 and 5.

Input: arr[] = {91, 93, 95}, K = 3
Output: -1
Explanation: 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.

LCM(arr) * ((10K-1)/LCM(arr))

Below is the implementation of the above approach.

C++
// 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
// 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
<script>

// JavaScript code to implement above approach 
function __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 array
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 Math.floor((a * b / __gcd(a, b)));
}

// Function to find the number
function 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 code
let arr = [ 2, 3, 5 ];
let K = 3;

document.write(findNum(arr, K));

// This code is contributed by Potta Lokesh

</script>

Output
990

Time Complexity: O(N*logD) where D is the maximum element of the array
Auxiliary Space: O(1)


 


Next Article
Article Tags :
Practice Tags :

Similar Reads

  翻译: