- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++

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,

## Example

#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; }

## Output

Game Started! X wins the Game

- Related Articles
- Predict the winner in Coin Game in C++
- Predict the Winner in C++
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?
- Taking the absolute sum of Array of Numbers in JavaScript
- C++ program to find winner of card game
- Program to find the winner of an array game using Python
- Maximum sum of absolute difference of any permutation in C++
- C++ Program to find winner of unique bidding game
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Find the winner by adding Pairwise difference of elements in the array until Possible in Python
- C++ Program to find winner name of stick crossing game
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++

Advertisements