LCM of an array of numbers in Java

L.C.M. or Least Common Multiple of two values, is the smallest positive value which is a multiple of both values.

For example, multiples of 3 and 4 are:

3 ? 3, 6, 9, 12, 15 ...

4 ? 4, 8, 12, 16, 20 ...

The smallest common multiple is 12, hence the LCM of 3 and 4 is 12.

Understanding LCM of Array

To find the LCM of an array of numbers, we can use the mathematical relationship: LCM(a, b) = (a × b) / GCD(a, b). For multiple numbers, we calculate LCM iteratively.

Method 1: Using GCD-Based Approach

// Function to find GCD of two numbers
function gcd(a, b) {
    if (b === 0) return a;
    return gcd(b, a % b);
}

// Function to find LCM of two numbers
function lcm(a, b) {
    return (a * b) / gcd(a, b);
}

// Function to find LCM of an array
function lcmOfArray(arr) {
    let result = arr[0];
    for (let i = 1; i < arr.length; i++) {
        result = lcm(result, arr[i]);
    }
    return result;
}

// Test with array
let numbers = [25, 50, 125, 625];
console.log("Array:", numbers);
console.log("LCM of array:", lcmOfArray(numbers));
Array: [ 25, 50, 125, 625 ]
LCM of array: 625

Method 2: Step-by-Step Calculation

function findLCMStep(arr) {
    console.log("Finding LCM step by step:");
    
    let result = arr[0];
    console.log("Starting with:", result);
    
    for (let i = 1; i < arr.length; i++) {
        let current = arr[i];
        let oldResult = result;
        
        // Calculate LCM of result and current number
        result = (result * current) / gcd(result, current);
        
        console.log(`LCM(${oldResult}, ${current}) = ${result}`);
    }
    
    return result;
}

function gcd(a, b) {
    return b === 0 ? a : gcd(b, a % b);
}

// Test array
let testArray = [12, 18, 24];
let finalLCM = findLCMStep(testArray);
console.log("Final LCM:", finalLCM);
Finding LCM step by step:
Starting with: 12
LCM(12, 18) = 36
LCM(36, 24) = 72
Final LCM: 72

Comparison

Method Time Complexity Space Complexity Readability
GCD-Based O(n × log(min(a,b))) O(1) High
Iterative O(n²) O(1) Medium

Edge Cases

// Handle edge cases
function safeLCM(arr) {
    if (!arr || arr.length === 0) return 0;
    if (arr.length === 1) return arr[0];
    
    // Filter out zeros and negative numbers
    let validNumbers = arr.filter(num => num > 0);
    
    if (validNumbers.length === 0) return 0;
    
    return lcmOfArray(validNumbers);
}

function lcmOfArray(arr) {
    let result = arr[0];
    for (let i = 1; i < arr.length; i++) {
        result = (result * arr[i]) / gcd(result, arr[i]);
    }
    return result;
}

function gcd(a, b) {
    return b === 0 ? a : gcd(b, a % b);
}

// Test edge cases
console.log("Empty array:", safeLCM([]));
console.log("Single element:", safeLCM([15]));
console.log("With zeros:", safeLCM([0, 12, 18]));
Empty array: 0
Single element: 15
With zeros: 36

Conclusion

The GCD-based approach is the most efficient method for finding LCM of an array. It uses the mathematical relationship between LCM and GCD to compute results iteratively with optimal time complexity.

Updated on: 2026-03-15T23:18:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements