Maximum GCD of all subarrays of length at least 2 | Function to find GCD ; To store the maximum GCD ; Traverse the array ; Find GCD of the consecutive element ; If calculated GCD > maxGCD then update it ; Print the maximum GCD ; Driver Code ; Function Call
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function findMaxGCD ( arr , n ) { let maxGCD = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let val = gcd ( arr [ i ] , arr [ i + 1 ] ) ; if ( val > maxGCD ) { maxGCD = val ; } } document . write ( maxGCD + " " ) ; } let arr = [ 3 , 18 , 9 , 9 , 5 , 15 , 8 , 7 , 6 , 9 ] ; let n = arr . length ; findMaxGCD ( arr , n ) ;