
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
GCD of More Than Two or Array Numbers in Java
In this article, we will learn to calculate the GCD of more than two numbers in Java. We will discuss two approaches, the recursive method and an optimized approach using the Euclidean algorithm.
The Greatest Common Divisor (GCD) of two or more integers is the largest integer that divides all the numbers without leaving a remainder. When it comes to an array of numbers, finding the GCD means determining the GCD of all the elements in the array.
Problem Statement
Given an array of integers, find the GCD of all the elements.
Example ?Input
{7, 49, 177, 105, 119, 42}
Output
1
Since 1 divides all the numbers in the array
Recursive Method
In this approach, we use Recursion to calculate the GCD of two numbers and then iteratively apply it to all elements in the array.
Following are the steps to calculate the GCD of two numbers in Java recursively ?
- Uses a recursive function to calculate the GCD of two numbers.
- Starts with the first two numbers in the array and calculates their GCD.
- Iteratively applies the GCD function to the result and the next element in the array.
- Stops early and returns 1 if the GCD becomes 1 at any step, as it is the smallest possible GCD.
Example
Below is the Java program for GCD of more than two numbers recursively ?
public class Demo{ static int gcd_of_nums(int val_1, int val_2){ if (val_1 == 0) return val_2; return gcd_of_nums(val_2 % val_1, val_1); } static int find_gcd(int arr[], int no){ int result = arr[0]; for (int i = 1; i < no; i++){ result = gcd_of_nums(arr[i], result); if(result == 1){ return 1; } } return result; } public static void main(String[] args){ int my_arr[] = { 7, 49, 177, 105, 119, 42}; int no = my_arr.length; System.out.println("The GCD of the elements in the array is "); System.out.println(find_gcd(my_arr, no)); } }
Output
The GCD of the elements in the array is 1
Optimized Iterative Euclidean Algorithm
The Euclidean algorithm can also be implemented iteratively to compute the GCD of two numbers. This approach avoids recursion, making it more efficient and stack-safe for large inputs.
Following are the steps to calculate the GCD of two numbers in Java iteratively ?
- Uses an iterative implementation of the Euclidean algorithm to calculate the GCD of two numbers.
- Starts with the first element as the initial GCD and iteratively updates it with each subsequent number in the array.
- Avoids recursion, making it stack-safe and more efficient for large inputs.
- Includes an early exit condition when the GCD is 1 to save computation time.
Example
Below is the Java program for GCD of more than two numbers iteratively?
public class OptimizedGCD { // Method to calculate GCD of two numbers iteratively static int gcd_iterative(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } // Method to find GCD of an array using the iterative method static int find_gcd_iterative(int arr[]) { int result = arr[0]; for (int i = 1; i < arr.length; i++) { result = gcd_iterative(result, arr[i]); if (result == 1) { // Early exit if GCD is 1 return 1; } } return result; } public static void main(String[] args) { int my_arr[] = { 7, 49, 177, 105, 119, 42 }; System.out.println("The GCD of the elements in the array using iterative method is "); System.out.println(find_gcd_iterative(my_arr)); } }
Output
The GCD of the elements in the array using the iterative method is
1
Comparison Table
Criteria |
Recursive Method | Iterative Method |
Complexity | O(n à log(max(arr))) | O(n à log(max(arr))) |
Use of Recursion |
Yes | No |
Stack Safety |
May cause stack overflow for large inputs | Safe for all inputs |
Performance |
Slightly slower due to recursive calls | Slightly faster due to the iterative nature |