Finding smallest number that satisfies some conditions in JavaScript

We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n-digit number which is a multiple of all the elements specified in the array.

If there exist no such n-digit element then we should return the smallest such element.

For example: If the array is ?

const arr = [12, 4, 5, 10, 9]

For both n = 2 and n = 3, we need to find the smallest number that is divisible by all array elements.

Understanding the Problem

The problem requires finding the LCM (Least Common Multiple) of array elements, then checking if it has n digits. If not, we find the smallest n-digit multiple of the LCM.

Example Implementation

The code for this will be ?

const arr = [12, 4, 5, 10, 9];
const num1 = 2;
const num2 = 3;

const allDivides = (arr, num) => arr.every(el => num % el === 0);

const smallestMultiple = (arr, num) => {
    let smallestN = Math.pow(10, (num - 1));
    while(!allDivides(arr, smallestN)){
        smallestN++;
    };
    return smallestN;
};

console.log(smallestMultiple(arr, num1));
console.log(smallestMultiple(arr, num2));

Output

The output in the console will be ?

180
180

Optimized Approach Using LCM

A more efficient approach calculates the LCM first, then finds the smallest n-digit multiple:

// Helper function to find GCD
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);

// Helper function to find LCM of two numbers
const lcm = (a, b) => (a * b) / gcd(a, b);

// Function to find LCM of array
const findLCM = arr => arr.reduce((acc, num) => lcm(acc, num));

const optimizedSmallestMultiple = (arr, n) => {
    const arrayLCM = findLCM(arr);
    const minNDigit = Math.pow(10, n - 1);
    
    // If LCM already has n or more digits
    if (arrayLCM >= minNDigit) {
        return arrayLCM;
    }
    
    // Find smallest n-digit multiple of LCM
    const multiplier = Math.ceil(minNDigit / arrayLCM);
    return arrayLCM * multiplier;
};

console.log("Optimized approach:");
console.log(optimizedSmallestMultiple([12, 4, 5, 10, 9], 2));
console.log(optimizedSmallestMultiple([12, 4, 5, 10, 9], 3));
Optimized approach:
180
180

How It Works

The algorithm works in these steps:

  1. Calculate the LCM of all array elements
  2. Check if LCM has at least n digits
  3. If yes, return LCM; if no, find the smallest n-digit multiple
  4. Use Math.ceil(minNDigit / LCM) to find the required multiplier

Comparison

Approach Time Complexity Efficiency
Brute Force O(result - minNDigit) Slow for large results
LCM-based O(array.length * log(max)) Much faster

Conclusion

The LCM-based approach is significantly more efficient than brute force iteration. It calculates the mathematical solution directly instead of checking each number sequentially.

Updated on: 2026-03-15T23:19:00+05:30

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements