- 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 in C++
Suppose we have an array of scores that are non-negative integers. The first player picks one of the numbers from either end of the array followed by the second player and then the first player and so on. Each time a player picks a number, that number will not be available for the other player. This continues until all the scores have been chosen. The player who has got the maximum score wins. So, if we have the scores array, we have to predict whether player 1 is the winner.
So, if the input is like [1, 5, 233, 7], then the output will be True, as the first player has chosen 1. Then the second player has to choose between 5 and 7. No matter which number the second player chooses, the first player, can choose 233. Finally, the first player has more score (234) than the second player (12), so we need to return true as the first player can win.
To solve this, we will follow these steps −
if n is same as 1, then −
return true
Define an array player1 of size: n x n, an array player2 of size n x n, and sum of size n x n.
for initialize i := 0, when i < n, update (increase i by 1), do −
for initialize j := 0, when j < n, update (increase j by 1), do −
player1[i, j] := -1
player2[i, j] := -1
for initialize i := 0, when i < n, update (increase i by 1), do −
for initialize j := i, when j < n, update (increase j by 1), do −
if i is same as j, then −
sum[i, j] := arr[i]
Otherwise
sum[i, j] := arr[j] + sum[i, j - 1]
for initialize length := 1, when length <= n, update (increase length by 1), do −
for initialize i := 0, when i + length - 1 < n, update (increase i by 1), do −
end := i + length - 1
if i + 1 <= end, then −
player1[i, end] := maximum of arr[i] + ((if player2[i + 1, end] is same as - 1, then 0, otherwise player2[i + 1, end])) and arr[end] + ((if player2[i, end - 1] is same as -1, then , otherwise player2[i, end - 1]))
Otherwise
player1[i, end] := arr[i]
player2[i, end] := sum[i, end] - player1[i, end]
return true when player1[0, n - 1] >= player2[0, n - 1], otherwise false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; typedef long long int lli; class Solution { public: lli solve(vector <int> arr, lli n){ if (n == 1) return true; lli player1[n][n], player2[n][n], sum[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { player1[i][j] = -1; player2[i][j] = -1; } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (i == j) { sum[i][j] = arr[i]; } else { sum[i][j] = arr[j] + sum[i][j - 1]; } } } for (int length = 1; length <= n; length++) { for (int i = 0; i + length - 1 < n; i++) { lli end = i + length - 1; if (i + 1 <= end) player1[i][end] = max(arr[i] + (player2[i + 1][end] == -1 ? 0 : player2[i + 1][end]), arr[end] + (player2[i][end - 1] == -1 ?: player2[i][end - 1])); else player1[i][end] = arr[i]; player2[i][end] = sum[i][end] - player1[i][end]; } } return player1[0][n - 1] >= player2[0][n - 1]; } bool PredictTheWinner(vector<int>& nums) { return solve(nums, nums.size()) ; } }; main(){ Solution ob; vector<int> v = {1, 5, 233, 7}; cout << (ob.PredictTheWinner(v)); }
Input
{1, 5, 233, 7}
Output
1
- Related Articles
- Predict the winner in Coin Game in C++
- Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++
- Maximum games played by winner in C++
- C++ code to find different winner and non-winner counts on a contest
- C++ code to find winner of math contest
- C++ program to find winner of card game
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ program to find winner of typing game after delay timing
- Is it possible to predict the occurrence of an earthquake?
- Find winner of an election where votes are represented as candidate names in C++
- C++ Program to find winner and final price in a second price auction event
- C++ code to check water pouring game has all winner or not
