Given an array **arr[]** of **N** integers and two players **A** and **B** are playing a game where the players pick the element with the maximum digit sum in their turns. In the end, the player with the maximum sum of the picked elements wins the game. Assuming that the player **A** always starts the game first and both the players play optimally, the task is to find the winner of the game.

**Examples:**

Input:arr[] = {12, 43, 25, 23, 30}

Output:B

A choses 43

B chooses 25

A chooses 23

B chooses 30

A chooses 12

A’s score = 43 + 23 + 12 = 78

B’s score = 25 + 30 = 55

Input:arr[] = {2, 1, 1, 2}

Output:Draw

**Approach:** Sort the array based on the digit sum values of the integers, if the digit sum of two integers is same then they will be compared based on their values, this is because the value will maximize the sum in the end. After the array has been sorted based on the custom comparator, player A will try to pick the elements starting from the greatest (greedily).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the ` `// sum of the digits of n ` `int` `digit_sum(` `int` `n) ` `{ ` ` ` `int` `s = 0; ` ` ` `while` `(n > 0) { ` ` ` `s += n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` `return` `s; ` `} ` ` ` `// Compares two integers according ` `// to their digit sum ` `bool` `comparator(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Sum of digits of a ` ` ` `int` `s1 = digit_sum(a); ` ` ` ` ` `// Sum of digits of b ` ` ` `int` `s2 = digit_sum(b); ` ` ` ` ` `// If the digit sum of a is equal ` ` ` `// to the digit sum of b ` ` ` `if` `(s1 == s2) ` ` ` `return` `(a < b); ` ` ` `return` `(s1 < s2); ` `} ` ` ` `// Function to return the winner of the game ` `string findTheWinner(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Sort the elements based on ` ` ` `// the digit sum values ` ` ` `sort(arr, arr + n, comparator); ` ` ` ` ` `// Find player A's score ` ` ` `int` `scoreA = 0; ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i -= 2) ` ` ` `scoreA += arr[i]; ` ` ` ` ` `// Find player A's score ` ` ` `int` `scoreB = 0; ` ` ` `for` `(` `int` `i = n - 2; i >= 0; i -= 2) ` ` ` `scoreB += arr[i]; ` ` ` ` ` `// Find the winner ` ` ` `if` `(scoreA == scoreB) ` ` ` `return` `"Draw"` `; ` ` ` `else` `if` `(scoreA > scoreB) ` ` ` `return` `"A"` `; ` ` ` `return` `"B"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 12, 43, 25, 23, 30 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << findTheWinner(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function that returns the ` `# sum of the digits of n ` `def` `digit_sum(n): ` ` ` ` ` `s ` `=` `0` `; ` ` ` `while` `n > ` `0` `: ` ` ` `s ` `+` `=` `n ` `%` `10` ` ` `n ` `/` `=` `10` ` ` ` ` `return` `s ` ` ` `# Function to return the winner ` `# of the game ` `def` `findTheWinner(arr, n): ` ` ` ` ` `# Sort the elements based on ` ` ` `# the digit sum values ` ` ` `arr.sort(key ` `=` `digit_sum) ` ` ` ` ` `# Find player A's score ` ` ` `scoreA ` `=` `0` ` ` `i ` `=` `n ` `-` `1` ` ` `while` `i >` `=` `0` `: ` ` ` `scoreA ` `+` `=` `arr[i] ` ` ` `i ` `-` `=` `2` ` ` ` ` `# Find player A's score ` ` ` `scoreB ` `=` `0` ` ` `i ` `=` `n ` `-` `2` ` ` `while` `i >` `=` `0` `: ` ` ` `scoreA ` `+` `=` `arr[i] ` ` ` `i ` `-` `=` `2` ` ` ` ` `# Find the winner ` ` ` `if` `scoreA ` `=` `=` `scoreB: ` ` ` `return` `"Draw"` ` ` `elif` `(scoreA > scoreB): ` ` ` `return` `"A"` ` ` ` ` `return` `"B"` ` ` `# Driver code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `12` `, ` `43` `, ` `25` `, ` `23` `, ` `30` `] ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `print` `(findTheWinner(arr, n)) ` ` ` `# This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

A

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the game with N piles of boxes
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find the winner of the game based on greater number of divisors
- Find the winner of the Game of removing odd or replacing even array elements
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Determine winner of the Game by arranging balls in a row
- Determine the winner of a game of deleting Characters from a String
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)

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.