Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | C ++ Program to implement 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findGCD ( int arr [ ] , int N ) { int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { result = gcd ( result , arr [ i ] ) ; if ( result == 1 ) { return 1 ; } } return result ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGCD ( arr , N ) ; return 0 ; }