smallestCommons

Medium · Number Theory · 3 test cases

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order.

Examples

smallestCommons([1, 5]) → 60
smallestCommons([5, 1]) → 60

Starter code

function smallestCommons(arr) {

}

Complexity of the optimal solution

Time O(n * log(k)), space O(n). Let n be the size of the range and k be the value of the numbers. The solution iterates through the range (n) and calculates the LCM at each step. The GCD calculation is logarithmic. Space is O(n) for the range array.

Solve smallestCommons in the browser