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
< ? php function printCoins ( & $ arr , $ n ) { $ oddSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 2 ) $ oddSum += $ arr [ $ i ] ; $ evenSum = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i += 2 ) $ evenSum += $ arr [ $ i ] ; $ start = ( ( $ oddSum > $ evenSum ) ? 0 : 1 ) ; for ( $ i = $ start ; $ i < $ n ; $ i += 2 ) echo $ arr [ $ i ] . " ▁ " ; } $ arr1 = array ( 8 , 15 , 3 , 7 ) ; $ n = sizeof ( $ arr1 ) ; printCoins ( $ arr1 , $ n ) ; echo " STRNEWLINE " ; $ arr2 = array ( 2 , 2 , 2 , 2 ) ; $ n = sizeof ( $ arr2 ) ; printCoins ( $ arr2 , $ n ) ; echo " STRNEWLINE " ; $ arr3 = array ( 20 , 30 , 2 , 2 , 2 , 10 ) ; $ n = sizeof ( $ arr3 ) ; printCoins ( $ arr3 , $ n ) ; ? >