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
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function getWinner ( arr , n ) { let gcd = arr [ 0 ] ; let maxEle = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { gcd = __gcd ( gcd , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } let totalMoves = parseInt ( maxEle / gcd , 10 ) - n ; if ( totalMoves % 2 == 1 ) return ' ' ; return ' ' ; } let arr = [ 5 , 6 , 7 ] ; let n = arr . length ; document . write ( getWinner ( arr , n ) ) ;