Minimum possible final health of the last monster in a game | Function to return the gcd of two numbers ; Function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; Driver Code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function solve ( health , n ) { let currentgcd = gcd ( health [ 0 ] , health [ 1 ] ) ; for ( let i = 2 ; i < n ; ++ i ) { currentgcd = gcd ( currentgcd , health [ i ] ) ; } return currentgcd ; } let health = [ 4 , 6 , 8 , 12 ] ; let n = health . length ; document . write ( solve ( health , n ) ) ;