Smallest K digit number divisible by all numbers in given array
Last Updated :
04 Apr, 2023
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
Recursive 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++
#include <bits/stdc++.h>
using namespace std;
int findLCM(vector< int > arr, int idx)
{
if (idx == arr.size() - 1) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
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;
}
int main()
{
vector< int > arr = { 2, 3, 5 };
int N = 3;
cout << findNum(arr, N);
return 0;
}
|
Java
class GFG {
static int __gcd( int a, int b) {
if (b == 0 ) {
return a;
}
return __gcd(b, a % b);
}
static int findLCM( int [] arr, int idx)
{
if (idx == arr.length - 1 ) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1 );
return (a * b / __gcd(a, b));
}
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;
}
public static void main(String args[])
{
int [] arr = { 2 , 3 , 5 };
int N = 3 ;
System.out.println(findNum(arr, N));
}
}
|
Python3
import math
def findLCM(arr, idx):
if (idx = = len (arr) - 1 ):
return arr[idx]
a = arr[idx]
b = findLCM(arr, idx + 1 )
return (a * b / / math.gcd(a, b))
def findNum(arr, N):
lcm = findLCM(arr, 0 )
ans = pow ( 10 , N - 1 ) / / lcm
ans = (ans + 1 ) * lcm
return ans
if __name__ = = "__main__" :
arr = [ 2 , 3 , 5 ]
N = 3
print (findNum(arr, N))
|
C#
using System;
class GFG {
static int __gcd( int a, int b) {
if (b == 0) {
return a;
}
return __gcd(b, a % b);
}
static int findLCM( int [] arr, int idx)
{
if (idx == arr.Length - 1) {
return arr[idx];
}
int a = arr[idx];
int b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
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;
}
public static void Main( string []args)
{
int [] arr = { 2, 3, 5 };
int N = 3;
Console.WriteLine(findNum(arr, N));
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (b == 0) {
return a;
}
return __gcd(b, a % b);
}
function findLCM(arr, idx)
{
if (idx == arr.length - 1) {
return arr[idx];
}
let a = arr[idx];
let b = findLCM(arr, idx + 1);
return (a * b / __gcd(a, b));
}
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;
}
let arr = [2, 3, 5];
let N = 3;
document.write(findNum(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 2: Iterative Approach
- Initialize a variable num to the smallest K digit number, which is 10^(K-1).
- Create a loop that continues until we find a number that is divisible by all the numbers in the array.
- Inside the loop, check if num is divisible by all the numbers in the array using the all() function in Python. If it is divisible, then we have found the required number and we can return it.
- If num is not divisible by all the numbers in the array, increment it by 1 and continue the loop.
- Return the smallest number that is divisible by all the numbers in the array.
C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int smallest_k_digit_num_divisible_by_all(vector< int > arr, int k) {
int num = pow (10, k - 1);
while ( true ) {
bool is_divisible = true ;
for ( int n : arr) {
if (num % n != 0) {
is_divisible = false ;
break ;
}
}
if (is_divisible) {
return num;
}
num++;
}
}
int main() {
vector< int > arr = {2, 3, 5, 7};
int k = 4;
int num = smallest_k_digit_num_divisible_by_all(arr, k);
cout << num << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int smallestKDigitNumDivisibleByAll(List<Integer> arr, int k) {
int num = ( int ) Math.pow( 10 , k - 1 );
while ( true ) {
boolean isDivisible = true ;
for ( int n : arr) {
if (num % n != 0 ) {
isDivisible = false ;
break ;
}
}
if (isDivisible) {
return num;
}
num++;
}
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>(Arrays.asList( 2 , 3 , 5 , 7 ));
int k = 4 ;
int num = smallestKDigitNumDivisibleByAll(arr, k);
System.out.println(num);
}
}
|
Python3
def smallest_k_digit_num_divisible_by_all(arr, k):
num = 10 * * (k - 1 )
while True :
if all (num % n = = 0 for n in arr):
return num
num + = 1
arr = [ 2 , 3 , 5 , 7 ]
k = 4
num = smallest_k_digit_num_divisible_by_all(arr, k)
print (num)
|
C#
using System;
using System.Collections.Generic;
class SmallestNumberDivisibleByAll {
static int
SmallestKDigitNumDivisibleByAll(List< int > arr, int k)
{
int num = ( int )Math.Pow(
10, k - 1);
while ( true ) {
bool isDivisible = true ;
foreach ( int n in arr)
{
if (num % n != 0) {
isDivisible = false ;
break ;
}
}
if (isDivisible) {
return num;
}
num++;
}
}
static void Main()
{
List< int > arr = new List< int >{ 2, 3, 5, 7 };
int k = 4;
int num = SmallestKDigitNumDivisibleByAll(arr, k);
Console.WriteLine(num);
}
}
|
Javascript
function smallest_k_digit_num_divisible_by_all(arr, k) {
let num = Math.pow(10, k - 1);
while ( true ) {
let is_divisible = true ;
for (let n of arr) {
if (num % n != 0) {
is_divisible = false ;
break ;
}
}
if (is_divisible) {
return num;
}
num++;
}
}
let arr = [2, 3, 5, 7];
let k = 4;
let num = smallest_k_digit_num_divisible_by_all(arr, k);
console.log(num);
|
Time complexity: O(N * K), where N is the number of elements in the input array and K is the number of digits in the smallest number that we need to find.
Auxiliary space: O(1).
Share your thoughts in the comments
Please Login to comment...