Coin game of two corners ( Greedy Approach ) | C # program to find coins to be picked to make sure that we never loose . ; 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
using System ; class GFG { static void printCoins ( int [ ] arr , int n ) { int oddSum = 0 ; for ( int i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; int evenSum = 0 ; for ( int i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; int start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( int i = start ; i < n ; i += 2 ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr1 = { 8 , 15 , 3 , 7 } ; int n = arr1 . Length ; printCoins ( arr1 , n ) ; Console . Write ( " STRNEWLINE " ) ; int [ ] arr2 = { 2 , 2 , 2 , 2 } ; n = arr2 . Length ; printCoins ( arr2 , n ) ; Console . Write ( " STRNEWLINE " ) ; int [ ] arr3 = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . Length ; printCoins ( arr3 , n ) ; } }