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 −

 Live Demo

#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

Updated on: 17-Nov-2020

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements