- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given an array of n numbers. And there are two players X and Y. Our task is to predict the winner of the game.

For player X to win the absolute difference of the sum of numbers by X and Y should be a multiple of 4. If it is not divisible by 4, then Y wins. Player X starts the game.

Let’s take an example to understand the problem,

Input: a[] = {3 6 9 12} Output: X Explaination: X selects 3 and 6 Y selects 12 and 9 |3+6 - 12+9| = 12, 12 is a multiple of 4.

To solve this problem, we will check if every element of the array is divisible by 4 and keep track on the remainder found when we divide a number by 4. If the occurrence of every remainder is even, then X wins. i.e. the absolute difference is divisible by 4.

Count of arr[i]%4 for every value 0, 1, 2 ,3 should be even.

Program to show the implementation of our algorithm,

#include <iostream> using namespace std; int playGame(int a[], int n) { int count[4] = {0,0,0,0}; for (int i = 0; i < n; i++) { for(int j = 0; j<4;j++){ if(a[i]%4 == j) count[j]++; } } if (count[0] % 2 == 0 && count[1] % 2 == 0 && count[2] % 2 == 0 && count[3] == 0) return 1; else return 2; } int main() { int a[] = { 4, 8, 5, 9 }; int n = sizeof(a) / sizeof(a[0]); cout<<"Game Started!\n"; if (playGame(a, n) == 1) cout << "X wins the Game"; else cout << "Y wins the Game"; return 0; }

Game Started! X wins the Game

- Related Questions & Answers
- Predict the winner in Coin Game in C++
- Predict the Winner in C++
- Taking the absolute sum of Array of Numbers in JavaScript
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?
- Program to find the winner of an array game using Python
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- C++ program to find winner of card game
- Find the winner by adding Pairwise difference of elements in the array until Possible in Python
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- Maximum sum of absolute difference of any permutation in C++
- Python Pandas - Filtering columns from a DataFrame on the basis of sum
- Grouping on the basis of object property JavaScript
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- MySQL query to ORDER BY records on the basis of modulus result
- Select the topmost record from a table ordered by desc on the basis of ID?

Advertisements