Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | Java program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive Call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Driver Code ; Given array arr [ ] ; Function Call
public class GCD { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( int arr [ ] , int N ) { int result = 0 ; for ( int element : arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( findGCD ( arr , N ) ) ; } }