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
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function solve ( $ health , $ n ) { $ currentgcd = gcd ( $ health [ 0 ] , $ health [ 1 ] ) ; for ( $ i = 2 ; $ i < $ n ; ++ $ i ) { $ currentgcd = gcd ( $ currentgcd , $ health [ $ i ] ) ; } return $ currentgcd ; } $ health = array ( 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ health ) ; echo solve ( $ health , $ n ) ; ? >