Coin game of two corners ( Greedy Approach ) | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Find sum of odd positioned coins ; Find sum of even positioned coins ; Print even or odd coins depending upon which sum is greater . ; Driver code
function printCoins ( arr , n ) { var oddSum = 0 ; for ( var i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; var evenSum = 0 ; for ( var i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; var start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( var i = start ; i < n ; i += 2 ) document . write ( arr [ i ] + " " ) ; } var arr1 = [ 8 , 15 , 3 , 7 ] var n = arr1 . length ; printCoins ( arr1 , n ) ; document . write ( " " ) ; var arr2 = [ 2 , 2 , 2 , 2 ] var n = arr2 . length ; printCoins ( arr2 , n ) ; document . write ( " " ) ; var arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] n = arr3 . length ; printCoins ( arr3 , n ) ;