Java ProgramsNumbersFind LCM of Multiple Numbers

Find LCM of Multiple Numbers in Java

intermediate·  Numbers  ·  Number Theory

Problem

The LCM of a whole group of numbers is the smallest number that every one of them divides into evenly — it can be built up by combining the LCM of just two numbers at a time.

Given an array of integers, find the least common multiple shared by all of them.

Input
[4, 6, 8]
Output
LCM: 24

Java Program

Java
public class LCMOfMultipleNumbers { static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int lcm(int a, int b) { return (a * b) / gcd(a, b); // lcm(a, b) * gcd(a, b) = a * b, solved for the lcm } public static void main(String[] args) { int[] nums = {4, 6, 8}; int result = nums[0]; for (int i = 1; i < nums.length; i++) { result = lcm(result, nums[i]); // fold the next number into the running LCM } System.out.println("LCM: " + result); } }

Output

LCM: 24

Core Logic

The LCM of a group of numbers is the same as folding the two-number LCM across the whole array, one element at a time — lcm(lcm(a, b), c) equals lcm(a, b, c).

How It Works
  1. 1result starts out holding the first element of the array.
  2. 2A loop visits every remaining element, replacing result with lcm(result, nums[i]) at each step.
  3. 3lcm(a, b) itself is computed via (a * b) / gcd(a, b), reusing the same GCD-based formula as the two-number version.
  4. 4After the last element has been folded in, result holds the LCM of the entire array.
For [4, 6, 8], lcm(4, 6) is 12, and lcm(12, 8) is 24 — the LCM shared by all three.
💡

Key Point: This works because LCM, like GCD, is associative — combining numbers two at a time, in any order, always arrives at the same final answer.

Complexity
Time Complexity: O(n × log(min pair))Space Complexity: O(1)

Why: lcm() is called once per remaining element, and each call's cost is dominated by its internal gcd() call, so the total scales with both the array's length and that per-call cost.

Key Concepts

running LCMGCD helperfor loop

Approach 2: Java 8

Java
import java.util.Arrays; public class LCMOfMultipleNumbersStream { static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void main(String[] args) { int[] nums = {4, 6, 8}; // Folds lcm() across every element, the same way the manual loop does int result = Arrays.stream(nums).reduce(LCMOfMultipleNumbersStream::lcm).getAsInt(); System.out.println("LCM: " + result); } }

Output

LCM: 24

Core Logic

The same fold-two-at-a-time idea is exactly what Stream.reduce() is built for — no manual loop variable is needed.

How It Works
  1. 1Arrays.stream(nums) converts the int[] into an IntStream.
  2. 2.reduce(...) combines every element using the given operator, carrying the running result forward the same way the manual loop's result variable does.
  3. 3The method reference ::lcm supplies the pairwise combining logic without writing a lambda body out longhand.
  4. 4.getAsInt() unwraps the OptionalInt that reduce() without an identity value returns.
Reducing [4, 6, 8] with lcm folds 4 and 6 down to 12 first, then folds that 12 with 8, landing on the same answer, 24.
💡

Key Point: This is the exact same pattern used to find the GCD of multiple numbers with streams — only the combining function changes from gcd to lcm.

Complexity
Time Complexity: O(n × log(min pair))Space Complexity: O(1)

Why: reduce() still calls lcm() once per remaining element, the same total work as the manual loop, just expressed as a stream fold.

Key Concepts

Streamreduce()method reference

Related Programs