Find the winner by adding Pairwise difference of elements in the array until Possible | Function to calculate gcd ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function getWinner ( $ arr , $ n ) { $ gcd = $ arr [ 0 ] ; $ maxEle = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ gcd = __gcd ( $ gcd , $ arr [ $ i ] ) ; $ maxEle = max ( $ maxEle , $ arr [ $ i ] ) ; } $ totalMoves = ( $ maxEle / $ gcd ) - $ n ; if ( $ totalMoves % 2 == 1 ) return ' A ' ; return ' B ' ; } $ arr = array ( 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) ; echo getWinner ( $ arr , $ n ) ; ? >