Find the winner of the Game
Last Updated :
06 Dec, 2022
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 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++
#include <bits/stdc++.h>
using namespace std;
int digit_sum( int n)
{
int s = 0;
while (n > 0) {
s += n % 10;
n /= 10;
}
return s;
}
bool comparator( int a, int b)
{
int s1 = digit_sum(a);
int s2 = digit_sum(b);
if (s1 == s2)
return (a < b);
return (s1 < s2);
}
string findTheWinner( int arr[], int n)
{
sort(arr, arr + n, comparator);
int scoreA = 0;
for ( int i = n - 1; i >= 0; i -= 2)
scoreA += arr[i];
int scoreB = 0;
for ( int i = n - 2; i >= 0; i -= 2)
scoreB += arr[i];
if (scoreA == scoreB)
return "Draw" ;
else if (scoreA > scoreB)
return "A" ;
return "B" ;
}
int main()
{
int arr[] = { 12, 43, 25, 23, 30 };
int n = sizeof (arr) / sizeof ( int );
cout << findTheWinner(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int digit_sum( int n)
{
int s = 0 ;
while (n > 0 ) {
s += n % 10 ;
n /= 10 ;
}
return s;
}
static String findTheWinner( int arr[], int n)
{
int arr1[][] = new int [arr.length][ 2 ];
for ( int i= 0 ;i<arr.length;i++){
arr1[i][ 0 ] = arr[i];
arr1[i][ 1 ] = digit_sum(arr[i]);
}
Arrays.sort(arr1,(a,b)->a[ 0 ]-b[ 0 ]);
int scoreA = 0 ;
for ( int i = n - 1 ; i >= 0 ; i -= 2 )
scoreA += arr1[i][ 0 ];
int scoreB = 0 ;
for ( int i = n - 2 ; i >= 0 ; i -= 2 )
scoreB += arr1[i][ 0 ];
if (scoreA == scoreB)
return "Draw" ;
else if (scoreA > scoreB)
return "A" ;
return "B" ;
}
public static void main (String[] args) {
int arr[] = { 12 , 43 , 25 , 23 , 30 };
int n = arr.length;
System.out.println(findTheWinner(arr, n));
}
}
|
Python3
def digit_sum(n):
s = 0 ;
while n > 0 :
s + = n % 10
n / = 10
return s
def findTheWinner(arr, n):
arr.sort(key = digit_sum)
scoreA = 0
i = n - 1
while i > = 0 :
scoreA + = arr[i]
i - = 2
scoreB = 0
i = n - 2
while i > = 0 :
scoreA + = arr[i]
i - = 2
if scoreA = = scoreB:
return "Draw"
elif (scoreA > scoreB):
return "A"
return "B"
if __name__ = = "__main__" :
arr = [ 12 , 43 , 25 , 23 , 30 ]
n = len (arr);
print (findTheWinner(arr, n))
|
C#
using System;
class GFG
{
static int digit_sum( int n)
{
int s = 0;
while (n > 0) {
s += n % 10;
n /= 10;
}
return s;
}
static bool comparator( int a, int b)
{
int s1 = digit_sum(a);
int s2 = digit_sum(b);
if (s1 == s2)
return (a < b);
return (s1 < s2);
}
static string findTheWinner( int []arr, int n)
{
Array.Sort(arr, new Comparison< int >(
(i1, i2) => digit_sum(i2).CompareTo( digit_sum(i1))));
int scoreA = 0;
for ( int i = n - 1; i >= 0; i -= 2)
scoreA += arr[i];
int scoreB = 0;
for ( int i = n - 2; i >= 0; i -= 2)
scoreB += arr[i];
if (scoreA == scoreB)
return "Draw" ;
else if (scoreA > scoreB)
return "A" ;
return "B" ;
}
static void Main() {
int []arr = { 12, 43, 25, 23, 30 };
int n = arr.Length;
Console.Write(findTheWinner(arr, n));
}
}
|
Javascript
<script>
function digit_sum(n)
{
var s = 0;
while (n > 0) {
s += n % 10;
n = parseInt(n/10);
}
return s;
}
function comparator(a, b)
{
var s1 = digit_sum(a);
var s2 = digit_sum(b);
if (s1 == s2)
return (a < b);
return (s1 < s2);
}
function findTheWinner(arr, n)
{
arr.sort(comparator);
var scoreA = 0;
for ( var i = n - 1; i >= 0; i -= 2)
scoreA += arr[i];
var scoreB = 0;
for ( var i = n - 2; i >= 0; i -= 2)
scoreB += arr[i];
if (scoreA == scoreB)
return "Draw" ;
else if (scoreA > scoreB)
return "A" ;
return "B" ;
}
var arr = [ 12, 43, 25, 23, 30 ];
var n = arr.length;
document.write( findTheWinner(arr, n));
</script>
|
Time Complexity: O(n*log(n)*d) where n is the size of the array and d is the maximum number of digits in any number present in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...