Consider a two player coin game where each player gets turn one by one. There is a row of even number of coins, and a player on his/her turn can pick a coin from any of the two corners of the row. The player that collects coins with more value wins the game. Develop a strategy for the player making the first turn, such he/she never looses the game.

Note that the strategy to pick maximum of two corners may not work. In the following example, first player looses the game when he/she uses strategy to pick maximum of two corners.

**Example:**

18 20 15 30 10 14 First Player picks 18, now row of coins is 20 15 30 10 14 Second player picks 20, now row of coins is 15 30 10 14 First Player picks 15, now row of coins is 30 10 14 Second player picks 30, now row of coins is 10 14 First Player picks 14, now row of coins is 10 Second player picks 10, game over. The total value collected by second player is more (20 + 30 + 10) compared to first player (18 + 15 + 14). So the second player wins.

Note that this problem is different from Optimal Strategy for a Game | DP-31. There the target is to get maximum value. Here the target is to not loose. We have a Greedy Strategy here. The idea is to count sum of values of all even coins and odd coins, compare the two values. The player that makes the first move can always make sure that the other player is never able to choose an even coin if sum of even coins is higher. Similarly, he/she can make sure that the other player is never able to choose an odd coin if sum of odd coins is higher.

**Example:**

18 20 15 30 10 14 Sum of odd coins = 18 + 15 + 10 = 43 Sum of even coins = 20 + 30 + 14 = 64. Since the sum of even coins is more, the first player decides to collect all even coins. He first picks 14, now the other player can only pick a coin (10 or 18). Whichever is picked the other player, the first player again gets an opportunity to pick an even coin and block all even coins.

## C++

`// CPP program to find coins to be picked to make sure ` `// that we never loose. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns optimal value possible that a player can collect ` `// from an array of coins of size n. Note than n must be even ` `void` `printCoins(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Find sum of odd positioned coins ` ` ` `int` `oddSum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i += 2) ` ` ` `oddSum += arr[i]; ` ` ` ` ` `// Find sum of even positioned coins ` ` ` `int` `evenSum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i += 2) ` ` ` `evenSum += arr[i]; ` ` ` ` ` `// Print even or odd coins depending upon ` ` ` `// which sum is greater. ` ` ` `int` `start = ((oddSum > evenSum) ? 0 : 1); ` ` ` `for` `(` `int` `i = start; i < n; i += 2) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 8, 15, 3, 7 }; ` ` ` `int` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `printCoins(arr1, n); ` ` ` `cout << endl; ` ` ` ` ` `int` `arr2[] = { 2, 2, 2, 2 }; ` ` ` `n = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` `printCoins(arr2, n); ` ` ` `cout << endl; ` ` ` ` ` `int` `arr3[] = { 20, 30, 2, 2, 2, 10 }; ` ` ` `n = ` `sizeof` `(arr3) / ` `sizeof` `(arr3[0]); ` ` ` `printCoins(arr3, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find coins to be ` `// picked to make sure that we never loose. ` `class` `GFG ` `{ ` ` ` `// Returns optimal value possible ` `// that a player can collect from ` `// an array of coins of size n. ` `// Note than n must be even ` `static` `void` `printCoins(` `int` `arr[], ` `int` `n) ` `{ ` `// Find sum of odd positioned coins ` `int` `oddSum = ` `0` `; ` `for` `(` `int` `i = ` `0` `; i < n; i += ` `2` `) ` ` ` `oddSum += arr[i]; ` ` ` `// Find sum of even positioned coins ` `int` `evenSum = ` `0` `; ` `for` `(` `int` `i = ` `0` `; i < n; i += ` `2` `) ` ` ` `evenSum += arr[i]; ` ` ` `// Print even or odd coins depending ` `// upon which sum is greater. ` `int` `start = ((oddSum > evenSum) ? ` `0` `: ` `1` `); ` `for` `(` `int` `i = start; i < n; i += ` `2` `) ` ` ` `System.out.print(arr[i]+` `" "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr1[] = { ` `8` `, ` `15` `, ` `3` `, ` `7` `}; ` ` ` `int` `n = arr1.length; ` ` ` `printCoins(arr1, n); ` ` ` `System.out.println(); ` ` ` ` ` `int` `arr2[] = { ` `2` `, ` `2` `, ` `2` `, ` `2` `}; ` ` ` `n = arr2.length; ` ` ` `printCoins(arr2, n); ` ` ` `System.out.println(); ` ` ` ` ` `int` `arr3[] = { ` `20` `, ` `30` `, ` `2` `, ` `2` `, ` `2` `, ` `10` `}; ` ` ` `n = arr3.length; ` ` ` `printCoins(arr3, n); ` `} ` `} ` ` ` `// This code is contributed by ChitraNayal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 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 ` `def` `printCoins(arr, n) : ` ` ` ` ` `oddSum ` `=` `0` ` ` ` ` `# Find sum of odd positioned coins ` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `2` `) : ` ` ` `oddSum ` `+` `=` `arr[i] ` ` ` ` ` `evenSum ` `=` `0` ` ` ` ` `# Find sum of even ` ` ` `# positioned coins ` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `2` `) : ` ` ` `evenSum ` `+` `=` `arr[i] ` ` ` ` ` `# Print even or odd ` ` ` `# coins depending upon ` ` ` `# which sum is greater. ` ` ` `if` `oddSum > evenSum : ` ` ` `start ` `=` `0` ` ` `else` `: ` ` ` `start ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(start, n, ` `2` `) : ` ` ` `print` `(arr[i], end ` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr1 ` `=` `[` `8` `, ` `15` `, ` `3` `, ` `7` `] ` ` ` `n ` `=` `len` `(arr1) ` ` ` `printCoins(arr1, n) ` ` ` `print` `() ` ` ` ` ` `arr2 ` `=` `[` `2` `, ` `2` `, ` `2` `, ` `2` `] ` ` ` `n ` `=` `len` `(arr2) ` ` ` `printCoins(arr2, n) ` ` ` `print` `() ` ` ` ` ` `arr3 ` `=` `[` `20` `, ` `30` `, ` `2` `, ` `2` `, ` `2` `, ` `10` `] ` ` ` `n ` `=` `len` `(arr3) ` ` ` `printCoins(arr3, n) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find coins to be ` `// picked to make sure that we never loose. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns optimal value possible ` `// that a player can collect from ` `// an array of coins of size n. ` `// Note than n must be even ` `static` `void` `printCoins(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` `// Find sum of odd positioned coins ` `int` `oddSum = 0; ` `for` `(` `int` `i = 0; i < n; i += 2) ` ` ` `oddSum += arr[i]; ` ` ` `// Find sum of even positioned coins ` `int` `evenSum = 0; ` `for` `(` `int` `i = 0; i < n; i += 2) ` ` ` `evenSum += arr[i]; ` ` ` `// Print even or odd coins depending ` `// upon which sum is greater. ` `int` `start = ((oddSum > evenSum) ? 0 : 1); ` `for` `(` `int` `i = start; i < n; i += 2) ` ` ` `Console.Write(arr[i]+` `" "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] arr1 = { 8, 15, 3, 7 }; ` ` ` `int` `n = arr1.Length; ` ` ` `printCoins(arr1, n); ` ` ` `Console.Write(` `"\n"` `); ` ` ` ` ` `int` `[] arr2 = { 2, 2, 2, 2 }; ` ` ` `n = arr2.Length; ` ` ` `printCoins(arr2, n); ` ` ` `Console.Write(` `"\n"` `); ` ` ` ` ` `int` `[] arr3 = { 20, 30, 2, 2, 2, 10 }; ` ` ` `n = arr3.Length; ` ` ` `printCoins(arr3, n); ` `} ` `} ` ` ` `// This code is contributed by ChitraNayal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `printCoins(&` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Find sum of odd positioned coins ` ` ` `$oddSum` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `+= 2) ` ` ` `$oddSum` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `// Find sum of even positioned coins ` ` ` `$evenSum` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `+= 2) ` ` ` `$evenSum` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `// Print even or odd coins depending ` ` ` `// upon which sum is greater. ` ` ` `$start` `= ((` `$oddSum` `> ` `$evenSum` `) ? 0 : 1); ` ` ` `for` `(` `$i` `= ` `$start` `; ` `$i` `< ` `$n` `; ` `$i` `+= 2) ` ` ` `echo` `$arr` `[` `$i` `].` `" "` `; ` `} ` ` ` `// Driver Code ` `$arr1` `= ` `array` `( 8, 15, 3, 7 ); ` `$n` `= sizeof(` `$arr1` `); ` `printCoins(` `$arr1` `, ` `$n` `); ` `echo` `"\n"` `; ` ` ` `$arr2` `= ` `array` `( 2, 2, 2, 2 ); ` `$n` `= sizeof(` `$arr2` `); ` `printCoins(` `$arr2` `, ` `$n` `); ` `echo` `"\n"` `; ` ` ` `$arr3` `= ` `array` `( 20, 30, 2, 2, 2, 10 ); ` `$n` `= sizeof(` `$arr3` `); ` `printCoins(` `$arr3` `, ` `$n` `); ` ` ` `// This code is contributed by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

15 7 2 2 30 2 10

## Recommended Posts:

- Combinatorial Game Theory | Set 2 (Game of Nim)
- Coin Change | DP-7
- Correctness of Greedy Algorithms
- Huffman Coding | Greedy Algo-3
- Top 20 Greedy Algorithms Interview Questions
- Graph Coloring | Set 2 (Greedy Algorithm)
- Boruvka's algorithm | Greedy Algo-9
- Job Sequencing Problem | Set 1 (Greedy Algorithm)
- Greedy Algorithm for Egyptian Fraction
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Activity Selection Problem | Greedy Algo-1
- Greedy Algorithm to find Minimum number of Coins
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Prim’s MST for Adjacency List Representation | Greedy Algo-6

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.