Java LCM Of Array

Java LCM Of Array | In this section, we will find the LCM of the array in the Java programming language. LCM represents Lowest Common Multiple.

Before seeing the LCM of an array, first, let us see how to calculate the LCM of 2 numbers in Java. Also see:- HCF of two numbers in Java

Steps to find LCM of array in Java:-

Step-1:- Take two numbers.
Step2:- List the multiples of two numbers.
Step3:- Find the common least multiple, which is called LCM.

The formula to find the LCM is:- LCM(a,b) = a*b/GCD(a,b)

Where:-
LCM:- Lowest Common Multiple
a, b:- Two numbers needed to find LCM
GCD:- Greatest Common Divisor

Steps to Find GCD(a,b):-

Step1:- Find the divisor of positive integer ‘a’.
Step2:- Find the divisor of positive integer ‘b’.
Step3:- List the common factors of ‘a’ and ‘b’.
Step4:- The highest divisor of ‘a’ and ‘b’ is GCD.

There are two methods to Find LCM,
1) By finding multiple
2) By finding GCD

Program to Find LCM of array in Java by Finding Multiples

import java.util.Arrays;

public class Main {

   public static long lcm(int[] elements) {
      long lcm = 1;
      int divisor = 2;

      while (true) {
         int counter = 0;
         boolean divisible = false;

         for (int i = 0; i < elements.length; i++) {
            if (elements[i] == 0) {
               return 0;
            } else if (elements[i] < 0) {
               elements[i] = elements[i] * (-1);
            }

            if (elements[i] == 1) {
               counter++;
            }

            if (elements[i] % divisor == 0) {
               divisible = true;
               elements[i] = elements[i] / divisor;
            }
         }

         if (divisible) {
            lcm = lcm * divisor;
         } else {
            divisor++;
         }

         if (counter == elements.length) {
            return lcm;
         }
      }
   }

   public static void main(String[] args) {
      int[] array1 = { 21, 17, 13, 19, 41 };
      System.out.println("Array: " + Arrays.toString(array1));
      System.out.println("LCM = " + lcm(array1));

      int[] array2 = { 9, 18, 27, 36, 54 };
      System.out.println("Array: " + Arrays.toString(array2));
      System.out.println("LCM = " + lcm(array2));
   }
}

Output:-

Array: [21, 17, 13, 19, 41]
LCM = 3615339
Array: [9, 18, 27, 36, 54]
LCM = 108

Java LCM Of Array using Recursion & GCD

import java.util.Arrays;

public class Main {

   public static int gcd(int num1, int num2) {
      return num2 == 0 ? num1 : gcd(num2, num1 % num2);
   }

   public static int lcm(int[] array, int index) {
      if (index == array.length - 1) {
         return array[index];
      }
      int a = array[index];
      int b = lcm(array, index + 1);
      return (a * b / gcd(a, b));
   }

   public static void main(String[] args) {
      int[] array1 = { 21, 17, 13, 19, 41 };
      System.out.println("Array: " + Arrays.toString(array1));
      System.out.println("LCM = " + lcm(array1, 0));

      int[] array2 = { 9, 18, 27, 36, 54 };
      System.out.println("Array: " + Arrays.toString(array2));
      System.out.println("LCM = " + lcm(array2, 0));
   }
}

Output:-

Array: [21, 17, 13, 19, 41]
LCM = 3615339
Array: [9, 18, 27, 36, 54]
LCM = 108

In the above program, we have used the recursion technique to find LCM of array in Java. In the main method, we have used the Arrays.toString() method of the Java Arrays class to display the array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *