Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
Given an array of N numbers. Two players X and Y play a game where at every step one player selects a number. One number can be selected only once. After all the numbers have been selected, player X wins if the absolute difference between the sum of numbers collected by X and Y is divisible by 4, else Y wins.
Note: Player X starts the game and numbers are selected optimally at every step.
Examples:
Input: a[] = {4, 8, 12, 16}
Output: X
X chooses 4
Y chooses 12
X chooses 8
Y chooses 16
|(4 + 8) – (12 + 16)| = |12 – 28| = 16 which is divisible by 4.
Hence, X winsInput: a[] = {7, 9, 1}
Output: Y
Approach: The following steps can be followed to solve the problem:
- Initialize count0, count1, count2 and count3 to 0.
- Iterate for every number in the array and increase the above counters accordingly if a[i] % 4 == 0, a[i] % 4 == 1, a[i] % 4 == 2 or a[i] % 4 == 3.
- If count0, count1, count2 and count3 are all even numbers then X wins else Y will win.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to decide the winner int decideWinner(int a[], int n) { int count0 = 0; int count1 = 0; int count2 = 0; int count3 = 0; // Iterate for all numbers in the array for (int i = 0; i < n; i++) { // Condition to count // If mod gives 0 if (a[i] % 4 == 0) count0++; // If mod gives 1 else if (a[i] % 4 == 1) count1++; // If mod gives 2 else if (a[i] % 4 == 2) count2++; // If mod gives 3 else if (a[i] % 4 == 3) count3++; } // Check the winning condition for X if (count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 == 0) return 1; else return 2; } // Driver code int main() { int a[] = { 4, 8, 5, 9 }; int n = sizeof(a) / sizeof(a[0]); if (decideWinner(a, n) == 1) cout << "X wins"; else cout << "Y wins"; return 0; } |
Java
// Java implementation of the approach
class GFG
{
// Function to decide the winner
static int decideWinner(int []a, int n)
{
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
// Iterate for all numbers in the array
for (int i = 0; i < n; i++)
{
// Condition to count
// If mod gives 0
if (a[i] % 4 == 0)
count0++;
// If mod gives 1
else if (a[i] % 4 == 1)
count1++;
// If mod gives 2
else if (a[i] % 4 == 2)
count2++;
// If mod gives 3
else if (a[i] % 4 == 3)
count3++;
}
// Check the winning condition for X
if (count0 % 2 == 0 && count1 % 2 == 0 &&
count2 % 2 == 0 && count3 == 0)
return 1;
else
return 2;
}
// Driver code
public static void main(String args[])
{
int []a = { 4, 8, 5, 9 };
int n = a.length;
if (decideWinner(a, n) == 1)
System.out.print("X wins");
else
System.out.print("Y wins");
}
}
// This code is contributed by Akanksha Rai
[tabby title="Python3"]
# Python3 implementation of the approach # Function to decide the winner def decideWinner(a, n): count0 = 0 count1 = 0 count2 = 0 count3 = 0 # Iterate for all numbers in the array for i in range(n): # Condition to count # If mod gives 0 if (a[i] % 4 == 0): count0 += 1 # If mod gives 1 elif (a[i] % 4 == 1): count1 += 1 # If mod gives 2 elif (a[i] % 4 == 2): count2 += 1 # If mod gives 3 elif (a[i] % 4 == 3): count3 += 1 # Check the winning condition for X if (count0 % 2 == 0 and count1 % 2 == 0 and count2 % 2 == 0 and count3 == 0): return 1 else: return 2 # Driver code a = [4, 8, 5, 9] n = len(a) if (decideWinner(a, n) == 1): print("X wins") else: print("Y wins") # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to decide the winner static int decideWinner(int []a, int n) { int count0 = 0; int count1 = 0; int count2 = 0; int count3 = 0; // Iterate for all numbers in the array for (int i = 0; i < n; i++) { // Condition to count // If mod gives 0 if (a[i] % 4 == 0) count0++; // If mod gives 1 else if (a[i] % 4 == 1) count1++; // If mod gives 2 else if (a[i] % 4 == 2) count2++; // If mod gives 3 else if (a[i] % 4 == 3) count3++; } // Check the winning condition for X if (count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 == 0) return 1; else return 2; } // Driver code public static void Main() { int []a = { 4, 8, 5, 9 }; int n = a.Length; if (decideWinner(a, n) == 1) Console.Write("X wins"); else Console.Write("Y wins"); } } // This code is contributed by Akanksha Rai |
PHP
<?php // PHP implementation of the approach // Function to decide the winner function decideWinner($a, $n) { $count0 = 0; $count1 = 0; $count2 = 0; $count3 = 0; // Iterate for all numbers in the array for ($i = 0; $i < $n; $i++) { // Condition to count // If mod gives 0 if ($a[$i] % 4 == 0) $count0++; // If mod gives 1 else if ($a[$i] % 4 == 1) $count1++; // If mod gives 2 else if ($a[$i] % 4 == 2) $count2++; // If mod gives 3 else if ($a[$i] % 4 == 3) $count3++; } // Check the winning condition for X if ($count0 % 2 == 0 && $count1 % 2 == 0 && $count2 % 2 == 0 && $count3 == 0) return 1; else return 2; } // Driver code $a = array( 4, 8, 5, 9 ); $n = count($a); if (decideWinner($a, $n) == 1) echo "X wins"; else echo "Y wins"; // This code is contributed by Ryuga ?> |
X wins
Recommended Posts:
- Predict the winner in Coin Game
- Predict the winner of the game | Sprague-Grundy
- Find the winner in nim-game
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Pair of prime numbers with a given sum and minimum absolute difference
- Find the winner of the game with N piles of boxes
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Absolute difference between the first X and last X Digits of N
- Minimum absolute difference between N and a power of 2
- Minimize the absolute difference of sum of two subsets
- Maximum absolute difference in an array
- Triplets in array with absolute difference less than k
- Maximum sum of absolute difference of an array
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.



